home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / reply.c.z / reply.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  102.0 KB  |  4,018 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    Main SMB reply routines
  5.    Copyright (C) Andrew Tridgell 1992-1998
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22.    This file handles most of the reply_ calls that the server
  23.    makes to handle specific protocols
  24. */
  25.  
  26.  
  27. #include "includes.h"
  28. #include "trans2.h"
  29. #include "nterr.h"
  30.  
  31. /* look in server.c for some explanation of these variables */
  32. extern int Protocol;
  33. extern int DEBUGLEVEL;
  34. extern int max_send;
  35. extern int max_recv;
  36. extern int chain_fnum;
  37. extern char magic_char;
  38. extern connection_struct Connections[];
  39. extern files_struct Files[];
  40. extern BOOL case_sensitive;
  41. extern BOOL case_preserve;
  42. extern BOOL short_case_preserve;
  43. extern pstring sesssetup_user;
  44. extern fstring myworkgroup;
  45. extern int Client;
  46. extern int global_oplock_break;
  47.  
  48. /* this macro should always be used to extract an fnum (smb_fid) from
  49. a packet to ensure chaining works correctly */
  50. #define GETFNUM(buf,where) (chain_fnum!= -1?chain_fnum:SVAL(buf,where))
  51.  
  52.  
  53. /****************************************************************************
  54. report a possible attack via the password buffer overflow bug
  55. ****************************************************************************/
  56. static void overflow_attack(int len)
  57. {
  58.     DEBUG(0,("%s: ERROR: Invalid password length %d\n", timestring(), len));
  59.     DEBUG(0,("your machine may be under attack by a user exploiting an old bug\n"));
  60.     DEBUG(0,("Attack was from IP=%s\n", client_addr()));
  61.     exit_server("possible attack");
  62. }
  63.  
  64.  
  65. /****************************************************************************
  66.   reply to an special message 
  67. ****************************************************************************/
  68. int reply_special(char *inbuf,char *outbuf)
  69. {
  70.     int outsize = 4;
  71.     int msg_type = CVAL(inbuf,0);
  72.     int msg_flags = CVAL(inbuf,1);
  73.     pstring name1,name2;
  74.     extern fstring remote_machine;
  75.     extern fstring local_machine;
  76.     int len;
  77.     char name_type = 0;
  78.     
  79.     *name1 = *name2 = 0;
  80.     
  81.     smb_setlen(outbuf,0);
  82.     
  83.     switch (msg_type) {
  84.     case 0x81: /* session request */
  85.         CVAL(outbuf,0) = 0x82;
  86.         CVAL(outbuf,3) = 0;
  87.         if (name_len(inbuf+4) > 50 || 
  88.             name_len(inbuf+4 + name_len(inbuf + 4)) > 50) {
  89.             DEBUG(0,("Invalid name length in session request\n"));
  90.             return(0);
  91.         }
  92.         name_extract(inbuf,4,name1);
  93.         name_extract(inbuf,4 + name_len(inbuf + 4),name2);
  94.         DEBUG(2,("netbios connect: name1=%s name2=%s\n",
  95.              name1,name2));      
  96.  
  97.         fstrcpy(remote_machine,name2);
  98.         remote_machine[15] = 0;
  99.         trim_string(remote_machine," "," ");
  100.         strlower(remote_machine);
  101.  
  102.         fstrcpy(local_machine,name1);
  103.         len = strlen(local_machine);
  104.         if (len == 16) {
  105.             name_type = local_machine[15];
  106.             local_machine[15] = 0;
  107.         }
  108.         trim_string(local_machine," "," ");
  109.         strlower(local_machine);
  110.  
  111.         if (name_type == 'R') {
  112.             /* We are being asked for a pathworks session --- 
  113.                no thanks! */
  114.             CVAL(outbuf, 0) = 0x83;
  115.             break;
  116.         }
  117.  
  118.         add_session_user(remote_machine);
  119.  
  120.         reload_services(True);
  121.         reopen_logs();
  122.  
  123.         break;
  124.         
  125.     case 0x89: /* session keepalive request 
  126.               (some old clients produce this?) */
  127.         CVAL(outbuf,0) = 0x85;
  128.         CVAL(outbuf,3) = 0;
  129.         break;
  130.         
  131.     case 0x82: /* positive session response */
  132.     case 0x83: /* negative session response */
  133.     case 0x84: /* retarget session response */
  134.         DEBUG(0,("Unexpected session response\n"));
  135.         break;
  136.         
  137.     case 0x85: /* session keepalive */
  138.     default:
  139.         return(0);
  140.     }
  141.     
  142.     DEBUG(5,("%s init msg_type=0x%x msg_flags=0x%x\n",
  143.          timestring(),msg_type,msg_flags));
  144.     
  145.     return(outsize);
  146. }
  147.  
  148.  
  149. /*******************************************************************
  150. work out what error to give to a failed connection
  151. ********************************************************************/
  152. static int connection_error(char *inbuf,char *outbuf,int connection_num)
  153. {
  154.   switch (connection_num)
  155.     {
  156.     case -8:
  157.       return(ERROR(ERRSRV,ERRnoresource));
  158.     case -7:
  159.       return(ERROR(ERRSRV,ERRbaduid));
  160.     case -6:
  161.       return(ERROR(ERRSRV,ERRinvdevice));
  162.     case -5:
  163.       return(ERROR(ERRSRV,ERRinvnetname));
  164.     case -4:
  165.       return(ERROR(ERRSRV,ERRaccess));
  166.     case -3:
  167.       return(ERROR(ERRDOS,ERRnoipc));
  168.     case -2:
  169.       return(ERROR(ERRSRV,ERRinvnetname));
  170.     }
  171.   return(ERROR(ERRSRV,ERRbadpw));
  172. }
  173.  
  174.  
  175.  
  176. /****************************************************************************
  177.   parse a share descriptor string
  178. ****************************************************************************/
  179. static void parse_connect(char *p,char *service,char *user,
  180.               char *password,int *pwlen,char *dev)
  181. {
  182.   char *p2;
  183.  
  184.   DEBUG(4,("parsing connect string %s\n",p));
  185.     
  186.   p2 = strrchr(p,'\\');
  187.   if (p2 == NULL)
  188.     fstrcpy(service,p);
  189.   else
  190.     fstrcpy(service,p2+1);
  191.   
  192.   p += strlen(p) + 2;
  193.   
  194.   fstrcpy(password,p);
  195.   *pwlen = strlen(password);
  196.  
  197.   p += strlen(p) + 2;
  198.  
  199.   fstrcpy(dev,p);
  200.   
  201.   *user = 0;
  202.   p = strchr(service,'%');
  203.   if (p != NULL)
  204.     {
  205.       *p = 0;
  206.       fstrcpy(user,p+1);
  207.     }
  208. }
  209.  
  210.  
  211.  
  212.  
  213. /****************************************************************************
  214.   reply to a tcon
  215. ****************************************************************************/
  216. int reply_tcon(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  217. {
  218.   pstring service;
  219.   pstring user;
  220.   pstring password;
  221.   pstring dev;
  222.   int connection_num;
  223.   int outsize = 0;
  224.   uint16 vuid = SVAL(inbuf,smb_uid);
  225.   int pwlen=0;
  226.  
  227.   *service = *user = *password = *dev = 0;
  228.  
  229.   parse_connect(smb_buf(inbuf)+1,service,user,password,&pwlen,dev);
  230.  
  231.   /*
  232.    * Pass the user through the NT -> unix user mapping
  233.    * function.
  234.    */
  235.   
  236.   (void)map_username(user);
  237.    
  238.   /*
  239.    * Do any UNIX username case mangling.
  240.    */
  241.   (void)Get_Pwnam( user, True);
  242.  
  243.   connection_num = make_connection(service,user,password,pwlen,dev,vuid);
  244.   
  245.   if (connection_num < 0)
  246.     return(connection_error(inbuf,outbuf,connection_num));
  247.   
  248.   outsize = set_message(outbuf,2,0,True);
  249.   SSVAL(outbuf,smb_vwv0,max_recv);
  250.   SSVAL(outbuf,smb_vwv1,connection_num);
  251.   SSVAL(outbuf,smb_tid,connection_num);
  252.   
  253.   DEBUG(3,("%s tcon service=%s user=%s cnum=%d\n",timestring(),service,user,connection_num));
  254.   
  255.   return(outsize);
  256. }
  257.  
  258.  
  259. /****************************************************************************
  260.   reply to a tcon and X
  261. ****************************************************************************/
  262. int reply_tcon_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  263. {
  264.   pstring service;
  265.   pstring user;
  266.   pstring password;
  267.   pstring devicename;
  268.   int connection_num;
  269.   uint16 vuid = SVAL(inbuf,smb_uid);
  270.   int passlen = SVAL(inbuf,smb_vwv3);
  271.  
  272.   *service = *user = *password = *devicename = 0;
  273.  
  274.   /* we might have to close an old one */
  275.   if ((SVAL(inbuf,smb_vwv2) & 0x1) != 0)
  276.     close_cnum(SVAL(inbuf,smb_tid),vuid);
  277.  
  278.   if (passlen > MAX_PASS_LEN) {
  279.       overflow_attack(passlen);
  280.   }
  281.   
  282.   {
  283.     char *path;
  284.     char *p;
  285.     memcpy(password,smb_buf(inbuf),passlen);
  286.     password[passlen]=0;    
  287.     path = smb_buf(inbuf) + passlen;
  288.  
  289.     if (passlen != 24) {
  290.       if (strequal(password," "))
  291.     *password = 0;
  292.       passlen = strlen(password);
  293.     }
  294.     
  295.     fstrcpy(service,path+2);
  296.     p = strchr(service,'\\');
  297.     if (!p)
  298.       return(ERROR(ERRSRV,ERRinvnetname));
  299.     *p = 0;
  300.     fstrcpy(service,p+1);
  301.     p = strchr(service,'%');
  302.     if (p)
  303.       {
  304.     *p++ = 0;
  305.     fstrcpy(user,p);
  306.       }
  307.     StrnCpy(devicename,path + strlen(path) + 1,6);
  308.     DEBUG(4,("Got device type %s\n",devicename));
  309.   }
  310.  
  311.   /*
  312.    * Pass the user through the NT -> unix user mapping
  313.    * function.
  314.    */
  315.   
  316.   (void)map_username(user);
  317.    
  318.   /*
  319.    * Do any UNIX username case mangling.
  320.    */
  321.   (void)Get_Pwnam( user, True);
  322.  
  323.   connection_num = make_connection(service,user,password,passlen,devicename,vuid);
  324.   
  325.   if (connection_num < 0)
  326.     return(connection_error(inbuf,outbuf,connection_num));
  327.  
  328.   if (Protocol < PROTOCOL_NT1)
  329.   {
  330.     set_message(outbuf,2,strlen(devicename)+1,True);
  331.     pstrcpy(smb_buf(outbuf),devicename);
  332.   }
  333.   else
  334.   {
  335.     char *fsname = "SAMBA";
  336.     char *p;
  337.  
  338.     set_message(outbuf,3,3,True);
  339.  
  340.     p = smb_buf(outbuf);
  341.     pstrcpy(p,devicename); p = skip_string(p,1); /* device name */
  342.     pstrcpy(p,fsname); p = skip_string(p,1); /* filesystem type e.g NTFS */
  343.  
  344.     set_message(outbuf,3,PTR_DIFF(p,smb_buf(outbuf)),False);
  345.  
  346.     SSVAL(outbuf, smb_vwv2, 0x0); /* optional support */
  347.   }
  348.   
  349.   DEBUG(3,("%s tconX service=%s user=%s cnum=%d\n",timestring(),service,user,connection_num));
  350.   
  351.   /* set the incoming and outgoing tid to the just created one */
  352.   SSVAL(inbuf,smb_tid,connection_num);
  353.   SSVAL(outbuf,smb_tid,connection_num);
  354.  
  355.   return chain_reply(inbuf,outbuf,length,bufsize);
  356. }
  357.  
  358.  
  359. /****************************************************************************
  360.   reply to an unknown type
  361. ****************************************************************************/
  362. int reply_unknown(char *inbuf,char *outbuf)
  363. {
  364.   int cnum;
  365.   int type;
  366.   cnum = SVAL(inbuf,smb_tid);
  367.   type = CVAL(inbuf,smb_com);
  368.   
  369.   DEBUG(0,("%s unknown command type (%s): cnum=%d type=%d (0x%X)\n",
  370.     timestring(),
  371.     smb_fn_name(type),
  372.     cnum,type,type));
  373.   
  374.   return(ERROR(ERRSRV,ERRunknownsmb));
  375. }
  376.  
  377.  
  378. /****************************************************************************
  379.   reply to an ioctl
  380. ****************************************************************************/
  381. int reply_ioctl(char *inbuf,char *outbuf, int size, int bufsize)
  382. {
  383.   DEBUG(3,("ignoring ioctl\n"));
  384. #if 0
  385.   /* we just say it succeeds and hope its all OK. 
  386.      some day it would be nice to interpret them individually */
  387.   return set_message(outbuf,1,0,True); 
  388. #else
  389.   return(ERROR(ERRSRV,ERRnosupport));
  390. #endif
  391. }
  392.  
  393.  
  394. /****************************************************************************
  395. reply to a session setup command
  396. ****************************************************************************/
  397. int reply_sesssetup_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  398. {
  399.   uint16 sess_vuid;
  400.   int gid;
  401.   int uid;
  402.   int   smb_bufsize;    
  403.   int   smb_mpxmax;     
  404.   int   smb_vc_num;     
  405.   uint32   smb_sesskey;    
  406.   int   smb_apasslen = 0;   
  407.   pstring smb_apasswd;
  408.   int   smb_ntpasslen = 0;   
  409.   pstring smb_ntpasswd;
  410.   BOOL valid_nt_password = False;
  411.   pstring user;
  412.   pstring orig_user;
  413.   BOOL guest=False;
  414.   BOOL computer_id=False;
  415.   static BOOL done_sesssetup = False;
  416.   BOOL doencrypt = SMBENCRYPT();
  417.   char *domain = "";
  418.  
  419.   *smb_apasswd = 0;
  420.   *smb_ntpasswd = 0;
  421.   
  422.   smb_bufsize = SVAL(inbuf,smb_vwv2);
  423.   smb_mpxmax = SVAL(inbuf,smb_vwv3);
  424.   smb_vc_num = SVAL(inbuf,smb_vwv4);
  425.   smb_sesskey = IVAL(inbuf,smb_vwv5);
  426.  
  427.   if (Protocol < PROTOCOL_NT1) {
  428.     smb_apasslen = SVAL(inbuf,smb_vwv7);
  429.     if (smb_apasslen > MAX_PASS_LEN)
  430.     {
  431.         overflow_attack(smb_apasslen);
  432.     }
  433.  
  434.     memcpy(smb_apasswd,smb_buf(inbuf),smb_apasslen);
  435.     smb_apasswd[smb_apasslen] = 0;
  436.     pstrcpy(user,smb_buf(inbuf)+smb_apasslen);
  437.  
  438.     if (!doencrypt && (lp_security() != SEC_SERVER)) {
  439.         smb_apasslen = strlen(smb_apasswd);
  440.     }
  441.   } else {
  442.     uint16 passlen1 = SVAL(inbuf,smb_vwv7);
  443.     uint16 passlen2 = SVAL(inbuf,smb_vwv8);
  444.     uint32 client_caps = IVAL(inbuf,smb_vwv11);
  445.     enum remote_arch_types ra_type = get_remote_arch();
  446.  
  447.     char *p = smb_buf(inbuf);    
  448.  
  449.     /* client_caps is used as final determination if client is NT or Win95. 
  450.        This is needed to return the correct error codes in some
  451.        circumstances.
  452.      */
  453.     
  454.     if(ra_type == RA_WINNT || ra_type == RA_WIN95)
  455.     {
  456.       if(client_caps & (CAP_NT_SMBS | CAP_STATUS32))
  457.         set_remote_arch( RA_WINNT);
  458.       else
  459.         set_remote_arch( RA_WIN95);
  460.     }
  461.  
  462.     if (passlen1 != 24 && passlen2 != 24)
  463.       doencrypt = False;
  464.  
  465.     if (passlen1 > MAX_PASS_LEN) {
  466.         overflow_attack(passlen1);
  467.     }
  468.  
  469.     passlen1 = MIN(passlen1, MAX_PASS_LEN);
  470.     passlen2 = MIN(passlen2, MAX_PASS_LEN);
  471.  
  472.     if(!doencrypt) {
  473.        /* both Win95 and WinNT stuff up the password lengths for
  474.           non-encrypting systems. Uggh. 
  475.       
  476.           if passlen1==24 its a win95 system, and its setting the
  477.           password length incorrectly. Luckily it still works with the
  478.           default code because Win95 will null terminate the password
  479.           anyway 
  480.  
  481.           if passlen1>0 and passlen2>0 then maybe its a NT box and its
  482.           setting passlen2 to some random value which really stuffs
  483.           things up. we need to fix that one.  */
  484.  
  485.       if (passlen1 > 0 && passlen2 > 0 && passlen2 != 24 && passlen2 != 1)
  486.         passlen2 = 0;
  487.     }
  488.  
  489.     if(doencrypt || (lp_security() == SEC_SERVER)) {
  490.       /* Save the lanman2 password and the NT md4 password. */
  491.       smb_apasslen = passlen1;
  492.       memcpy(smb_apasswd,p,smb_apasslen);
  493.       smb_apasswd[smb_apasslen] = 0;
  494.       smb_ntpasslen = passlen2;
  495.       memcpy(smb_ntpasswd,p+passlen1,smb_ntpasslen);
  496.       smb_ntpasswd[smb_ntpasslen] = 0;
  497.     } else {
  498.       /* we use the first password that they gave */
  499.       smb_apasslen = passlen1;
  500.       StrnCpy(smb_apasswd,p,smb_apasslen);      
  501.       
  502.       /* trim the password */
  503.       smb_apasslen = strlen(smb_apasswd);
  504.  
  505.       /* wfwg sometimes uses a space instead of a null */
  506.       if (strequal(smb_apasswd," ")) {
  507.     smb_apasslen = 0;
  508.     *smb_apasswd = 0;
  509.       }
  510.     }
  511.     
  512.     p += passlen1 + passlen2;
  513.     fstrcpy(user,p); p = skip_string(p,1);
  514.     domain = p;
  515.  
  516.     DEBUG(3,("Domain=[%s]  NativeOS=[%s] NativeLanMan=[%s]\n",
  517.          domain,skip_string(p,1),skip_string(p,2)));
  518.   }
  519.  
  520.  
  521.   DEBUG(3,("sesssetupX:name=[%s]\n",user));
  522.  
  523.   /* If name ends in $ then I think it's asking about whether a */
  524.   /* computer with that name (minus the $) has access. For now */
  525.   /* say yes to everything ending in $. */
  526.   if (user[strlen(user) - 1] == '$')
  527.   {
  528. #ifdef NTDOMAIN
  529.     struct smb_passwd *smb_pass; /* To check if machine account exists */
  530. /* 
  531.    PAXX: Ack. We don't want to do this. The workstation trust account
  532.    with a $ on the end should exist in the local password database
  533.    or be mapped to something generic, but not modified. For NT
  534.    domain support we must reject this used in certain circumstances
  535.    with a code to indicate to the client that it is an invalid use
  536.    of a workstation trust account. NTWKS needs this error to join
  537.    a domain. This may be the source of future bugs if we cannot
  538.    be sure whether to reject this or not.
  539. */
  540.    /* non-null user name indicates search by username not by smb userid */
  541.    smb_pass = get_smbpwd_entry(user, 0);
  542.  
  543.    if (!smb_pass)
  544.    {
  545.      /* lkclXXXX: if workstation entry doesn't exist, indicate logon failure */
  546.      DEBUG(4,("Workstation trust account %s doesn't exist.",user));
  547.      SSVAL(outbuf, smb_flg2, 0xc003); /* PAXX: Someone please unhack this */
  548.      CVAL(outbuf, smb_reh) = 1; /* PAXX: Someone please unhack this */
  549.      return(ERROR(NT_STATUS_LOGON_FAILURE, 0xc000)); /* decimal 109 NT error, 0xc000 */
  550.    }
  551.    else
  552.    {
  553.      /* PAXX: This is the NO LOGON workstation trust account stuff */
  554.      /* lkclXXXX: if the workstation *does* exist, indicate failure differently! */
  555.      DEBUG(4,("No Workstation trust account %s",user));
  556.      SSVAL(outbuf, smb_flg2, 0xc003); /* PAXX: Someone please unhack this */
  557.      CVAL(outbuf, smb_reh) = 1; /* PAXX: Someone please unhack this */
  558.      return(ERROR(NT_STATUS_NOLOGON_WORKSTATION_TRUST_ACCOUNT, 0xc000)); /* decimal 409 NT error, 0xc000 */
  559.    }
  560.  
  561.    computer_id = True;
  562. #else /* not NTDOMAIN, leave this in. PAXX: Someone get rid of this */
  563.     user[strlen(user) - 1] = '\0';
  564. #endif
  565.   }
  566.  
  567.  
  568.   /* If no username is sent use the guest account */
  569.   if (!*user)
  570.     {
  571.       pstrcpy(user,lp_guestaccount(-1));
  572.       /* If no user and no password then set guest flag. */
  573.       if( *smb_apasswd == 0)
  574.         guest = True;
  575.     }
  576.  
  577.   strlower(user);
  578.  
  579.   /* 
  580.    * In share level security, only overwrite sesssetup_use if
  581.    * it's a non null-session share. Helps keep %U and %G
  582.    * working.
  583.    */
  584.  
  585.   if((lp_security() != SEC_SHARE) || *user)
  586.     pstrcpy(sesssetup_user,user);
  587.  
  588.   reload_services(True);
  589.  
  590.    /*
  591.     * Save the username before mapping. We will use
  592.     * the original username sent to us for security=server
  593.     * checking.
  594.     */
  595.  
  596.    pstrcpy( orig_user, user);
  597.  
  598.  
  599.   /*
  600.    * Pass the user through the NT -> unix user mapping
  601.    * function.
  602.    */
  603.   
  604.   (void)map_username(user);
  605.    
  606.   /*
  607.    * Do any UNIX username case mangling.
  608.    */
  609.   (void)Get_Pwnam( user, True);
  610.  
  611.   add_session_user(user);
  612.  
  613.   /* Check if the given username was the guest user with no password.
  614.      We need to do this check after add_session_user() as that
  615.      call can potentially change the username (via map_user).
  616.    */
  617.  
  618.   if(!guest && strequal(user,lp_guestaccount(-1)) && (*smb_apasswd == 0))
  619.     guest = True;
  620.  
  621.   if (!guest && !(lp_security() == SEC_SERVER && 
  622.       /* Check with orig_user for security=server. */
  623.       server_validate(orig_user, domain, 
  624.                       smb_apasswd, smb_apasslen, 
  625.                       smb_ntpasswd, smb_ntpasslen)) &&
  626.       !check_hosts_equiv(user))
  627.     {
  628.  
  629.       /* now check if it's a valid username/password */
  630.       /* If an NT password was supplied try and validate with that
  631.      first. This is superior as the passwords are mixed case 
  632.          128 length unicode */
  633.       if(smb_ntpasslen)
  634.     {
  635.       if(!password_ok(user,smb_ntpasswd,smb_ntpasslen,NULL))
  636.         DEBUG(0,("NT Password did not match ! Defaulting to Lanman\n"));
  637.       else
  638.         valid_nt_password = True;
  639.     } 
  640.       if (!valid_nt_password && !password_ok(user,smb_apasswd,smb_apasslen,NULL))
  641.     {
  642.       if (!computer_id && lp_security() >= SEC_USER) {
  643. #if (GUEST_SESSSETUP == 0)
  644.         return(ERROR(ERRSRV,ERRbadpw));
  645. #endif
  646. #if (GUEST_SESSSETUP == 1)
  647.         if (Get_Pwnam(user,True))
  648.           return(ERROR(ERRSRV,ERRbadpw));
  649. #endif
  650.       }
  651.        if (*smb_apasswd || !Get_Pwnam(user,True))
  652.         pstrcpy(user,lp_guestaccount(-1));
  653.       DEBUG(3,("Registered username %s for guest access\n",user));
  654.       guest = True;
  655.     }
  656.     }
  657.  
  658.   if (!Get_Pwnam(user,True)) {
  659.     DEBUG(3,("No such user %s - using guest account\n",user));
  660.     pstrcpy(user,lp_guestaccount(-1));
  661.     guest = True;
  662.   }
  663.  
  664.   if (!strequal(user,lp_guestaccount(-1)) &&
  665.       lp_servicenumber(user) < 0)      
  666.     {
  667.       int homes = lp_servicenumber(HOMES_NAME);
  668.       char *home = get_home_dir(user);
  669.       if (homes >= 0 && home)
  670.     lp_add_home(user,homes,home);
  671.     }
  672.  
  673.  
  674.   /* it's ok - setup a reply */
  675.   if (Protocol < PROTOCOL_NT1) {
  676.     set_message(outbuf,3,0,True);
  677.   } else {
  678.     char *p;
  679.     set_message(outbuf,3,3,True);
  680.     p = smb_buf(outbuf);
  681.     pstrcpy(p,"Unix"); p = skip_string(p,1);
  682.     pstrcpy(p,"Samba "); pstrcat(p,VERSION); p = skip_string(p,1);
  683.     pstrcpy(p,myworkgroup); p = skip_string(p,1);
  684.     set_message(outbuf,3,PTR_DIFF(p,smb_buf(outbuf)),False);
  685.     /* perhaps grab OS version here?? */
  686.   }
  687.  
  688.   /* Set the correct uid in the outgoing and incoming packets
  689.      We will use this on future requests to determine which
  690.      user we should become.
  691.      */
  692.   {
  693.     struct passwd *pw = Get_Pwnam(user,False);
  694.     if (!pw) {
  695.       DEBUG(1,("Username %s is invalid on this system\n",user));
  696.       return(ERROR(ERRSRV,ERRbadpw));
  697.     }
  698.     gid = pw->pw_gid;
  699.     uid = pw->pw_uid;
  700.   }
  701.  
  702.   if (guest && !computer_id)
  703.     SSVAL(outbuf,smb_vwv2,1);
  704.  
  705.   /* register the name and uid as being validated, so further connections
  706.      to a uid can get through without a password, on the same VC */
  707.   sess_vuid = register_vuid(uid,gid,user,sesssetup_user,guest);
  708.  
  709.   SSVAL(outbuf,smb_uid,sess_vuid);
  710.   SSVAL(inbuf,smb_uid,sess_vuid);
  711.  
  712.   if (!done_sesssetup)
  713.     max_send = MIN(max_send,smb_bufsize);
  714.  
  715.   DEBUG(6,("Client requested max send size of %d\n", max_send));
  716.  
  717.   done_sesssetup = True;
  718.  
  719.   return chain_reply(inbuf,outbuf,length,bufsize);
  720. }
  721.  
  722.  
  723. /****************************************************************************
  724.   reply to a chkpth
  725. ****************************************************************************/
  726. int reply_chkpth(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  727. {
  728.   int outsize = 0;
  729.   int cnum,mode;
  730.   pstring name;
  731.   BOOL ok = False;
  732.   BOOL bad_path = False;
  733.  
  734.   cnum = SVAL(inbuf,smb_tid);
  735.   
  736.   pstrcpy(name,smb_buf(inbuf) + 1);
  737.   unix_convert(name,cnum,0,&bad_path);
  738.  
  739.   mode = SVAL(inbuf,smb_vwv0);
  740.  
  741.   if (check_name(name,cnum))
  742.     ok = directory_exist(name,NULL);
  743.  
  744.   if (!ok)
  745.   {
  746.     /* We special case this - as when a Windows machine
  747.        is parsing a path is steps through the components
  748.        one at a time - if a component fails it expects
  749.        ERRbadpath, not ERRbadfile.
  750.      */
  751.     if(errno == ENOENT)
  752.     {
  753.       unix_ERR_class = ERRDOS;
  754.       unix_ERR_code = ERRbadpath;
  755.     }
  756.  
  757. #if 0
  758.     /* Ugly - NT specific hack - maybe not needed ? (JRA) */
  759.     if((errno == ENOTDIR) && (Protocol >= PROTOCOL_NT1) &&
  760.        (get_remote_arch() == RA_WINNT))
  761.     {
  762.       unix_ERR_class = ERRDOS;
  763.       unix_ERR_code = ERRbaddirectory;
  764.     }
  765. #endif
  766.  
  767.     return(UNIXERROR(ERRDOS,ERRbadpath));
  768.   }
  769.  
  770.   outsize = set_message(outbuf,0,0,True);
  771.   
  772.   DEBUG(3,("%s chkpth %s cnum=%d mode=%d\n",timestring(),name,cnum,mode));
  773.   
  774.   return(outsize);
  775. }
  776.  
  777.  
  778. /****************************************************************************
  779.   reply to a getatr
  780. ****************************************************************************/
  781. int reply_getatr(char *inbuf,char *outbuf, int in_size, int buffsize)
  782. {
  783.   pstring fname;
  784.   int cnum;
  785.   int outsize = 0;
  786.   struct stat sbuf;
  787.   BOOL ok = False;
  788.   int mode=0;
  789.   uint32 size=0;
  790.   time_t mtime=0;
  791.   BOOL bad_path = False;
  792.  
  793.   cnum = SVAL(inbuf,smb_tid);
  794.  
  795.   pstrcpy(fname,smb_buf(inbuf) + 1);
  796.   unix_convert(fname,cnum,0,&bad_path);
  797.  
  798.   /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
  799.      under WfWg - weird! */
  800.   if (! (*fname))
  801.     {
  802.       mode = aHIDDEN | aDIR;
  803.       if (!CAN_WRITE(cnum)) mode |= aRONLY;
  804.       size = 0;
  805.       mtime = 0;
  806.       ok = True;
  807.     }
  808.   else
  809.     if (check_name(fname,cnum))
  810.     {
  811.       if (sys_stat(fname,&sbuf) == 0)
  812.       {
  813.         mode = dos_mode(cnum,fname,&sbuf);
  814.         size = sbuf.st_size;
  815.         mtime = sbuf.st_mtime;
  816.         if (mode & aDIR)
  817.           size = 0;
  818.         ok = True;
  819.       }
  820.       else
  821.         DEBUG(3,("stat of %s failed (%s)\n",fname,strerror(errno)));
  822.     }
  823.   
  824.   if (!ok)
  825.   {
  826.     if((errno == ENOENT) && bad_path)
  827.     {
  828.       unix_ERR_class = ERRDOS;
  829.       unix_ERR_code = ERRbadpath;
  830.     }
  831.  
  832.     return(UNIXERROR(ERRDOS,ERRbadfile));
  833.   }
  834.  
  835.   outsize = set_message(outbuf,10,0,True);
  836.  
  837.   SSVAL(outbuf,smb_vwv0,mode);
  838.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  839.     put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
  840.   else
  841.     put_dos_date3(outbuf,smb_vwv1,mtime);
  842.   SIVAL(outbuf,smb_vwv3,size);
  843.  
  844.   if (Protocol >= PROTOCOL_NT1) {
  845.     char *p = strrchr(fname,'/');
  846.     uint16 flg2 = SVAL(outbuf,smb_flg2);
  847.     if (!p) p = fname;
  848.     if (!is_8_3(fname, True))
  849.       SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
  850.   }
  851.   
  852.   DEBUG(3,("%s getatr name=%s mode=%d size=%d\n",timestring(),fname,mode,size));
  853.   
  854.   return(outsize);
  855. }
  856.  
  857.  
  858. /****************************************************************************
  859.   reply to a setatr
  860. ****************************************************************************/
  861. int reply_setatr(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  862. {
  863.   pstring fname;
  864.   int cnum;
  865.   int outsize = 0;
  866.   BOOL ok=False;
  867.   int mode;
  868.   time_t mtime;
  869.   BOOL bad_path = False;
  870.  
  871.   cnum = SVAL(inbuf,smb_tid);
  872.   
  873.   pstrcpy(fname,smb_buf(inbuf) + 1);
  874.   unix_convert(fname,cnum,0,&bad_path);
  875.  
  876.   mode = SVAL(inbuf,smb_vwv0);
  877.   mtime = make_unix_date3(inbuf+smb_vwv1);
  878.   
  879.   if (directory_exist(fname,NULL))
  880.     mode |= aDIR;
  881.   if (check_name(fname,cnum))
  882.     ok =  (dos_chmod(cnum,fname,mode,NULL) == 0);
  883.   if (ok)
  884.     ok = set_filetime(cnum,fname,mtime);
  885.   
  886.   if (!ok)
  887.   {
  888.     if((errno == ENOENT) && bad_path)
  889.     {
  890.       unix_ERR_class = ERRDOS;
  891.       unix_ERR_code = ERRbadpath;
  892.     }
  893.  
  894.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  895.   }
  896.  
  897.   outsize = set_message(outbuf,0,0,True);
  898.   
  899.   DEBUG(3,("%s setatr name=%s mode=%d\n",timestring(),fname,mode));
  900.   
  901.   return(outsize);
  902. }
  903.  
  904.  
  905. /****************************************************************************
  906.   reply to a dskattr
  907. ****************************************************************************/
  908. int reply_dskattr(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  909. {
  910.   int cnum;
  911.   int outsize = 0;
  912.   int dfree,dsize,bsize;
  913.   
  914.   cnum = SVAL(inbuf,smb_tid);
  915.   
  916.   sys_disk_free(".",&bsize,&dfree,&dsize);
  917.   
  918.   outsize = set_message(outbuf,5,0,True);
  919.   
  920.   SSVAL(outbuf,smb_vwv0,dsize);
  921.   SSVAL(outbuf,smb_vwv1,bsize/512);
  922.   SSVAL(outbuf,smb_vwv2,512);
  923.   SSVAL(outbuf,smb_vwv3,dfree);
  924.   
  925.   DEBUG(3,("%s dskattr cnum=%d dfree=%d\n",timestring(),cnum,dfree));
  926.   
  927.   return(outsize);
  928. }
  929.  
  930.  
  931. /****************************************************************************
  932.   reply to a search
  933.   Can be called from SMBsearch, SMBffirst or SMBfunique.
  934. ****************************************************************************/
  935. int reply_search(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  936. {
  937.   pstring mask;
  938.   pstring directory;
  939.   pstring fname;
  940.   int size,mode;
  941.   time_t date;
  942.   int dirtype;
  943.   int cnum;
  944.   int outsize = 0;
  945.   int numentries = 0;
  946.   BOOL finished = False;
  947.   int maxentries;
  948.   int i;
  949.   char *p;
  950.   BOOL ok = False;
  951.   int status_len;
  952.   char *path;
  953.   char status[21];
  954.   int dptr_num= -1;
  955.   BOOL check_descend = False;
  956.   BOOL expect_close = False;
  957.   BOOL can_open = True;
  958.   BOOL bad_path = False;
  959.  
  960.   *mask = *directory = *fname = 0;
  961.  
  962.   /* If we were called as SMBffirst then we must expect close. */
  963.   if(CVAL(inbuf,smb_com) == SMBffirst)
  964.     expect_close = True;
  965.   
  966.   cnum = SVAL(inbuf,smb_tid);
  967.  
  968.   outsize = set_message(outbuf,1,3,True);
  969.   maxentries = SVAL(inbuf,smb_vwv0); 
  970.   dirtype = SVAL(inbuf,smb_vwv1);
  971.   path = smb_buf(inbuf) + 1;
  972.   status_len = SVAL(smb_buf(inbuf),3 + strlen(path));
  973.  
  974.   
  975.   /* dirtype &= ~aDIR; */
  976.   
  977.   DEBUG(5,("path=%s status_len=%d\n",path,status_len));
  978.  
  979.   
  980.   if (status_len == 0)
  981.     {
  982.       pstring dir2;
  983.  
  984.       pstrcpy(directory,smb_buf(inbuf)+1);
  985.       pstrcpy(dir2,smb_buf(inbuf)+1);
  986.       unix_convert(directory,cnum,0,&bad_path);
  987.       unix_format(dir2);
  988.  
  989.       if (!check_name(directory,cnum))
  990.         can_open = False;
  991.  
  992.       p = strrchr(dir2,'/');
  993.       if (p == NULL) 
  994.       {
  995.         pstrcpy(mask,dir2);
  996.         *dir2 = 0;
  997.       }
  998.       else
  999.       {
  1000.         *p = 0;
  1001.         pstrcpy(mask,p+1);
  1002.       }
  1003.  
  1004.       p = strrchr(directory,'/');
  1005.       if (!p) 
  1006.         *directory = 0;
  1007.       else
  1008.         *p = 0;
  1009.  
  1010.       if (strlen(directory) == 0)
  1011.         pstrcpy(directory,"./");
  1012.       bzero(status,21);
  1013.       CVAL(status,0) = dirtype;
  1014.     }
  1015.   else
  1016.     {
  1017.       memcpy(status,smb_buf(inbuf) + 1 + strlen(path) + 4,21);
  1018.       memcpy(mask,status+1,11);
  1019.       mask[11] = 0;
  1020.       dirtype = CVAL(status,0) & 0x1F;
  1021.       Connections[cnum].dirptr = dptr_fetch(status+12,&dptr_num);      
  1022.       if (!Connections[cnum].dirptr)
  1023.     goto SearchEmpty;
  1024.       string_set(&Connections[cnum].dirpath,dptr_path(dptr_num));
  1025.       if (!case_sensitive)
  1026.     strnorm(mask);
  1027.     }
  1028.  
  1029.   /* turn strings of spaces into a . */  
  1030.   {
  1031.     trim_string(mask,NULL," ");
  1032.     if ((p = strrchr(mask,' ')))
  1033.       {
  1034.     fstring ext;
  1035.     fstrcpy(ext,p+1);
  1036.     *p = 0;
  1037.     trim_string(mask,NULL," ");
  1038.     pstrcat(mask,".");
  1039.     pstrcat(mask,ext);
  1040.       }
  1041.   }
  1042.  
  1043.   /* Convert the formatted mask. (This code lives in trans2.c) */
  1044.   mask_convert(mask);
  1045.  
  1046.   {
  1047.     int skip;
  1048.  
  1049.     p = mask;
  1050.     while(*p)
  1051.     {
  1052.       if((skip = skip_multibyte_char( *p )) != 0 )
  1053.       {
  1054.         p += skip;
  1055.       }
  1056.       else
  1057.       {
  1058.         if (*p != '?' && *p != '*' && !isdoschar(*p))
  1059.         {
  1060.           DEBUG(5,("Invalid char [%c] in search mask?\n",*p));
  1061.           *p = '?';
  1062.         }
  1063.         p++;
  1064.       }
  1065.     }
  1066.   }
  1067.  
  1068.   if (!strchr(mask,'.') && strlen(mask)>8)
  1069.     {
  1070.       fstring tmp;
  1071.       fstrcpy(tmp,&mask[8]);
  1072.       mask[8] = '.';
  1073.       mask[9] = 0;
  1074.       pstrcat(mask,tmp);
  1075.     }
  1076.  
  1077.   DEBUG(5,("mask=%s directory=%s\n",mask,directory));
  1078.   
  1079.   if (can_open)
  1080.     {
  1081.       p = smb_buf(outbuf) + 3;
  1082.       
  1083.       ok = True;
  1084.       
  1085.       if (status_len == 0)
  1086.     {
  1087.       dptr_num = dptr_create(cnum,directory,expect_close,SVAL(inbuf,smb_pid));
  1088.       if (dptr_num < 0)
  1089.         {
  1090.           if(dptr_num == -2)
  1091.           {
  1092.             if((errno == ENOENT) && bad_path)
  1093.             {
  1094.               unix_ERR_class = ERRDOS;
  1095.               unix_ERR_code = ERRbadpath;
  1096.             }
  1097.             return (UNIXERROR(ERRDOS,ERRnofids));
  1098.           }
  1099.           return(ERROR(ERRDOS,ERRnofids));
  1100.         }
  1101.     }
  1102.  
  1103.       DEBUG(4,("dptr_num is %d\n",dptr_num));
  1104.  
  1105.       if (ok)
  1106.     {
  1107.       if ((dirtype&0x1F) == aVOLID)
  1108.         {      
  1109.           memcpy(p,status,21);
  1110.           make_dir_struct(p,"???????????",volume_label(SNUM(cnum)),0,aVOLID,0);
  1111.           dptr_fill(p+12,dptr_num);
  1112.           if (dptr_zero(p+12) && (status_len==0))
  1113.         numentries = 1;
  1114.           else
  1115.         numentries = 0;
  1116.           p += DIR_STRUCT_SIZE;
  1117.         }
  1118.       else 
  1119.         {
  1120.           DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",Connections[cnum].dirpath,lp_dontdescend(SNUM(cnum))));
  1121.           if (in_list(Connections[cnum].dirpath,
  1122.               lp_dontdescend(SNUM(cnum)),True))
  1123.         check_descend = True;
  1124.  
  1125.           for (i=numentries;(i<maxentries) && !finished;i++)
  1126.         {
  1127.           finished = 
  1128.             !get_dir_entry(cnum,mask,dirtype,fname,&size,&mode,&date,check_descend);
  1129.           if (!finished)
  1130.             {
  1131.               memcpy(p,status,21);
  1132.               make_dir_struct(p,mask,fname,size,mode,date);
  1133.               dptr_fill(p+12,dptr_num);
  1134.               numentries++;
  1135.             }
  1136.           p += DIR_STRUCT_SIZE;
  1137.         }
  1138.         }
  1139.     }
  1140.     }
  1141.  
  1142.  
  1143.  SearchEmpty:
  1144.  
  1145.   if (numentries == 0 || !ok)
  1146.     {
  1147.       CVAL(outbuf,smb_rcls) = ERRDOS;
  1148.       SSVAL(outbuf,smb_err,ERRnofiles);
  1149.     }
  1150.  
  1151.   /* If we were called as SMBffirst with smb_search_id == NULL
  1152.      and no entries were found then return error and close dirptr 
  1153.      (X/Open spec) */
  1154.  
  1155.   if(ok && expect_close && numentries == 0 && status_len == 0)
  1156.     {
  1157.       CVAL(outbuf,smb_rcls) = ERRDOS;
  1158.       SSVAL(outbuf,smb_err,ERRnofiles);
  1159.       /* Also close the dptr - we know it's gone */
  1160.       dptr_close(dptr_num);
  1161.     }
  1162.  
  1163.   /* If we were called as SMBfunique, then we can close the dirptr now ! */
  1164.   if(dptr_num >= 0 && CVAL(inbuf,smb_com) == SMBfunique)
  1165.     dptr_close(dptr_num);
  1166.  
  1167.   SSVAL(outbuf,smb_vwv0,numentries);
  1168.   SSVAL(outbuf,smb_vwv1,3 + numentries * DIR_STRUCT_SIZE);
  1169.   CVAL(smb_buf(outbuf),0) = 5;
  1170.   SSVAL(smb_buf(outbuf),1,numentries*DIR_STRUCT_SIZE);
  1171.  
  1172.   if (Protocol >= PROTOCOL_NT1) {
  1173.     uint16 flg2 = SVAL(outbuf,smb_flg2);
  1174.     SSVAL(outbuf,smb_flg2,flg2 | 0x40); /* IS_LONG_NAME */
  1175.   }
  1176.   
  1177.   outsize += DIR_STRUCT_SIZE*numentries;
  1178.   smb_setlen(outbuf,outsize - 4);
  1179.   
  1180.   if ((! *directory) && dptr_path(dptr_num))
  1181.     slprintf(directory, sizeof(directory)-1, "(%s)",dptr_path(dptr_num));
  1182.  
  1183.   DEBUG(4,("%s %s mask=%s path=%s cnum=%d dtype=%d nument=%d of %d\n",
  1184.     timestring(),
  1185.     smb_fn_name(CVAL(inbuf,smb_com)), 
  1186.     mask,directory,cnum,dirtype,numentries,maxentries));
  1187.  
  1188.   return(outsize);
  1189. }
  1190.  
  1191.  
  1192. /****************************************************************************
  1193.   reply to a fclose (stop directory search)
  1194. ****************************************************************************/
  1195. int reply_fclose(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1196. {
  1197.   int cnum;
  1198.   int outsize = 0;
  1199.   int status_len;
  1200.   char *path;
  1201.   char status[21];
  1202.   int dptr_num= -1;
  1203.  
  1204.   cnum = SVAL(inbuf,smb_tid);
  1205.  
  1206.   outsize = set_message(outbuf,1,0,True);
  1207.   path = smb_buf(inbuf) + 1;
  1208.   status_len = SVAL(smb_buf(inbuf),3 + strlen(path));
  1209.  
  1210.   
  1211.   if (status_len == 0)
  1212.     return(ERROR(ERRSRV,ERRsrverror));
  1213.  
  1214.   memcpy(status,smb_buf(inbuf) + 1 + strlen(path) + 4,21);
  1215.  
  1216.   if(dptr_fetch(status+12,&dptr_num)) {
  1217.     /*  Close the dptr - we know it's gone */
  1218.     dptr_close(dptr_num);
  1219.   }
  1220.  
  1221.   SSVAL(outbuf,smb_vwv0,0);
  1222.  
  1223.   DEBUG(3,("%s search close cnum=%d\n",timestring(),cnum));
  1224.  
  1225.   return(outsize);
  1226. }
  1227.  
  1228.  
  1229. /****************************************************************************
  1230.   reply to an open
  1231. ****************************************************************************/
  1232. int reply_open(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1233. {
  1234.   pstring fname;
  1235.   int cnum;
  1236.   int fnum = -1;
  1237.   int outsize = 0;
  1238.   int fmode=0;
  1239.   int share_mode;
  1240.   int size = 0;
  1241.   time_t mtime=0;
  1242.   int unixmode;
  1243.   int rmode=0;
  1244.   struct stat sbuf;
  1245.   BOOL bad_path = False;
  1246.   files_struct *fsp;
  1247.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1248.  
  1249.   cnum = SVAL(inbuf,smb_tid);
  1250.  
  1251.   share_mode = SVAL(inbuf,smb_vwv0);
  1252.  
  1253.   pstrcpy(fname,smb_buf(inbuf)+1);
  1254.   unix_convert(fname,cnum,0,&bad_path);
  1255.     
  1256.   fnum = find_free_file();
  1257.   if (fnum < 0)
  1258.     return(ERROR(ERRSRV,ERRnofids));
  1259.  
  1260.   if (!check_name(fname,cnum))
  1261.   {
  1262.     if((errno == ENOENT) && bad_path)
  1263.     {
  1264.       unix_ERR_class = ERRDOS;
  1265.       unix_ERR_code = ERRbadpath;
  1266.     }
  1267.     Files[fnum].reserved = False;
  1268.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1269.   }
  1270.  
  1271.   unixmode = unix_mode(cnum,aARCH);
  1272.       
  1273.   open_file_shared(fnum,cnum,fname,share_mode,3,unixmode,
  1274.                    oplock_request,&rmode,NULL);
  1275.  
  1276.   fsp = &Files[fnum];
  1277.  
  1278.   if (!fsp->open)
  1279.   {
  1280.     if((errno == ENOENT) && bad_path)
  1281.     {
  1282.       unix_ERR_class = ERRDOS;
  1283.       unix_ERR_code = ERRbadpath;
  1284.     }
  1285.     Files[fnum].reserved = False;
  1286.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1287.   }
  1288.  
  1289.   if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
  1290.     close_file(fnum,False);
  1291.     return(ERROR(ERRDOS,ERRnoaccess));
  1292.   }
  1293.     
  1294.   size = sbuf.st_size;
  1295.   fmode = dos_mode(cnum,fname,&sbuf);
  1296.   mtime = sbuf.st_mtime;
  1297.  
  1298.   if (fmode & aDIR) {
  1299.     DEBUG(3,("attempt to open a directory %s\n",fname));
  1300.     close_file(fnum,False);
  1301.     return(ERROR(ERRDOS,ERRnoaccess));
  1302.   }
  1303.   
  1304.   outsize = set_message(outbuf,7,0,True);
  1305.   SSVAL(outbuf,smb_vwv0,fnum);
  1306.   SSVAL(outbuf,smb_vwv1,fmode);
  1307.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  1308.     put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
  1309.   else
  1310.     put_dos_date3(outbuf,smb_vwv2,mtime);
  1311.   SIVAL(outbuf,smb_vwv4,size);
  1312.   SSVAL(outbuf,smb_vwv6,rmode);
  1313.  
  1314.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1315.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1316.   }
  1317.     
  1318.   if(fsp->granted_oplock)
  1319.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1320.   return(outsize);
  1321. }
  1322.  
  1323.  
  1324. /****************************************************************************
  1325.   reply to an open and X
  1326. ****************************************************************************/
  1327. int reply_open_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  1328. {
  1329.   pstring fname;
  1330.   int cnum = SVAL(inbuf,smb_tid);
  1331.   int fnum = -1;
  1332.   int smb_mode = SVAL(inbuf,smb_vwv3);
  1333.   int smb_attr = SVAL(inbuf,smb_vwv5);
  1334.   /* Breakout the oplock request bits so we can set the
  1335.      reply bits separately. */
  1336.   BOOL ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
  1337.   BOOL core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1338.   BOOL oplock_request = ex_oplock_request | core_oplock_request;
  1339. #if 0
  1340.   int open_flags = SVAL(inbuf,smb_vwv2);
  1341.   int smb_sattr = SVAL(inbuf,smb_vwv4); 
  1342.   uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
  1343. #endif
  1344.   int smb_ofun = SVAL(inbuf,smb_vwv8);
  1345.   int unixmode;
  1346.   int size=0,fmode=0,mtime=0,rmode=0;
  1347.   struct stat sbuf;
  1348.   int smb_action = 0;
  1349.   BOOL bad_path = False;
  1350.   files_struct *fsp;
  1351.  
  1352.   /* If it's an IPC, pass off the pipe handler. */
  1353.   if (IS_IPC(cnum))
  1354.     return reply_open_pipe_and_X(inbuf,outbuf,length,bufsize);
  1355.  
  1356.   /* XXXX we need to handle passed times, sattr and flags */
  1357.  
  1358.   pstrcpy(fname,smb_buf(inbuf));
  1359.   unix_convert(fname,cnum,0,&bad_path);
  1360.     
  1361.   fnum = find_free_file();
  1362.   if (fnum < 0)
  1363.     return(ERROR(ERRSRV,ERRnofids));
  1364.  
  1365.   if (!check_name(fname,cnum))
  1366.   {
  1367.     if((errno == ENOENT) && bad_path)
  1368.     {
  1369.       unix_ERR_class = ERRDOS;
  1370.       unix_ERR_code = ERRbadpath;
  1371.     }
  1372.     Files[fnum].reserved = False;
  1373.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1374.   }
  1375.  
  1376.   unixmode = unix_mode(cnum,smb_attr | aARCH);
  1377.       
  1378.   open_file_shared(fnum,cnum,fname,smb_mode,smb_ofun,unixmode,
  1379.            oplock_request, &rmode,&smb_action);
  1380.       
  1381.   fsp = &Files[fnum];
  1382.  
  1383.   if (!fsp->open)
  1384.   {
  1385.     if((errno == ENOENT) && bad_path)
  1386.     {
  1387.       unix_ERR_class = ERRDOS;
  1388.       unix_ERR_code = ERRbadpath;
  1389.     }
  1390.     Files[fnum].reserved = False;
  1391.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1392.   }
  1393.  
  1394.   if (fstat(fsp->fd_ptr->fd,&sbuf) != 0) {
  1395.     close_file(fnum,False);
  1396.     return(ERROR(ERRDOS,ERRnoaccess));
  1397.   }
  1398.  
  1399.   size = sbuf.st_size;
  1400.   fmode = dos_mode(cnum,fname,&sbuf);
  1401.   mtime = sbuf.st_mtime;
  1402.   if (fmode & aDIR) {
  1403.     close_file(fnum,False);
  1404.     return(ERROR(ERRDOS,ERRnoaccess));
  1405.   }
  1406.  
  1407.   /* If the caller set the extended oplock request bit
  1408.      and we granted one (by whatever means) - set the
  1409.      correct bit for extended oplock reply.
  1410.    */
  1411.  
  1412.   if (ex_oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1413.     smb_action |= EXTENDED_OPLOCK_GRANTED;
  1414.   }
  1415.  
  1416.   if(ex_oplock_request && fsp->granted_oplock) {
  1417.     smb_action |= EXTENDED_OPLOCK_GRANTED;
  1418.   }
  1419.  
  1420.   /* If the caller set the core oplock request bit
  1421.      and we granted one (by whatever means) - set the
  1422.      correct bit for core oplock reply.
  1423.    */
  1424.  
  1425.   if (core_oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1426.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1427.   }
  1428.  
  1429.   if(core_oplock_request && fsp->granted_oplock) {
  1430.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1431.   }
  1432.  
  1433.   set_message(outbuf,15,0,True);
  1434.   SSVAL(outbuf,smb_vwv2,fnum);
  1435.   SSVAL(outbuf,smb_vwv3,fmode);
  1436.   if(lp_dos_filetime_resolution(SNUM(cnum)) )
  1437.     put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
  1438.   else
  1439.     put_dos_date3(outbuf,smb_vwv4,mtime);
  1440.   SIVAL(outbuf,smb_vwv6,size);
  1441.   SSVAL(outbuf,smb_vwv8,rmode);
  1442.   SSVAL(outbuf,smb_vwv11,smb_action);
  1443.  
  1444.   chain_fnum = fnum;
  1445.  
  1446.   return chain_reply(inbuf,outbuf,length,bufsize);
  1447. }
  1448.  
  1449.  
  1450. /****************************************************************************
  1451.   reply to a SMBulogoffX
  1452. ****************************************************************************/
  1453. int reply_ulogoffX(char *inbuf,char *outbuf,int length,int bufsize)
  1454. {
  1455.   uint16 vuid = SVAL(inbuf,smb_uid);
  1456.   user_struct *vuser = get_valid_user_struct(vuid);
  1457.  
  1458.   if(vuser == 0) {
  1459.     DEBUG(3,("ulogoff, vuser id %d does not map to user.\n", vuid));
  1460.   }
  1461.  
  1462.   /* in user level security we are supposed to close any files
  1463.      open by this user */
  1464.   if ((vuser != 0) && (lp_security() != SEC_SHARE)) {
  1465.     int i;
  1466.     for (i=0;i<MAX_OPEN_FILES;i++)
  1467.       if ((Files[i].vuid == vuid) && Files[i].open) {
  1468.     close_file(i,False);
  1469.       }
  1470.   }
  1471.  
  1472.   invalidate_vuid(vuid);
  1473.  
  1474.   set_message(outbuf,2,0,True);
  1475.  
  1476.   DEBUG(3,("%s ulogoffX vuid=%d\n",timestring(),vuid));
  1477.  
  1478.   return chain_reply(inbuf,outbuf,length,bufsize);
  1479. }
  1480.  
  1481.  
  1482. /****************************************************************************
  1483.   reply to a mknew or a create
  1484. ****************************************************************************/
  1485. int reply_mknew(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1486. {
  1487.   pstring fname;
  1488.   int cnum,com;
  1489.   int fnum = -1;
  1490.   int outsize = 0;
  1491.   int createmode;
  1492.   mode_t unixmode;
  1493.   int ofun = 0;
  1494.   BOOL bad_path = False;
  1495.   files_struct *fsp;
  1496.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1497.  
  1498.   com = SVAL(inbuf,smb_com);
  1499.   cnum = SVAL(inbuf,smb_tid);
  1500.  
  1501.   createmode = SVAL(inbuf,smb_vwv0);
  1502.   pstrcpy(fname,smb_buf(inbuf)+1);
  1503.   unix_convert(fname,cnum,0,&bad_path);
  1504.  
  1505.   if (createmode & aVOLID)
  1506.     {
  1507.       DEBUG(0,("Attempt to create file (%s) with volid set - please report this\n",fname));
  1508.     }
  1509.   
  1510.   unixmode = unix_mode(cnum,createmode);
  1511.   
  1512.   fnum = find_free_file();
  1513.   if (fnum < 0)
  1514.     return(ERROR(ERRSRV,ERRnofids));
  1515.  
  1516.   if (!check_name(fname,cnum))
  1517.   {
  1518.     if((errno == ENOENT) && bad_path)
  1519.     {
  1520.       unix_ERR_class = ERRDOS;
  1521.       unix_ERR_code = ERRbadpath;
  1522.     }
  1523.     Files[fnum].reserved = False;
  1524.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1525.   }
  1526.  
  1527.   if(com == SMBmknew)
  1528.   {
  1529.     /* We should fail if file exists. */
  1530.     ofun = 0x10;
  1531.   }
  1532.   else
  1533.   {
  1534.     /* SMBcreate - Create if file doesn't exist, truncate if it does. */
  1535.     ofun = 0x12;
  1536.   }
  1537.  
  1538.   /* Open file in dos compatibility share mode. */
  1539.   open_file_shared(fnum,cnum,fname,(DENY_FCB<<4)|0xF, ofun, unixmode, 
  1540.                    oplock_request, NULL, NULL);
  1541.   
  1542.   fsp = &Files[fnum];
  1543.  
  1544.   if (!fsp->open)
  1545.   {
  1546.     if((errno == ENOENT) && bad_path) 
  1547.     {
  1548.       unix_ERR_class = ERRDOS;
  1549.       unix_ERR_code = ERRbadpath;
  1550.     }
  1551.     Files[fnum].reserved = False;
  1552.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1553.   }
  1554.  
  1555.   outsize = set_message(outbuf,1,0,True);
  1556.   SSVAL(outbuf,smb_vwv0,fnum);
  1557.  
  1558.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1559.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1560.   }
  1561.  
  1562.   if(fsp->granted_oplock)
  1563.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1564.  
  1565.   DEBUG(2,("new file %s\n",fname));
  1566.   DEBUG(3,("%s mknew %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
  1567.   
  1568.   return(outsize);
  1569. }
  1570.  
  1571.  
  1572. /****************************************************************************
  1573.   reply to a create temporary file
  1574. ****************************************************************************/
  1575. int reply_ctemp(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1576. {
  1577.   pstring fname;
  1578.   pstring fname2;
  1579.   int cnum;
  1580.   int fnum = -1;
  1581.   int outsize = 0;
  1582.   int createmode;
  1583.   mode_t unixmode;
  1584.   BOOL bad_path = False;
  1585.   files_struct *fsp;
  1586.   int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
  1587.  
  1588.   cnum = SVAL(inbuf,smb_tid);
  1589.   createmode = SVAL(inbuf,smb_vwv0);
  1590.   pstrcpy(fname,smb_buf(inbuf)+1);
  1591.   pstrcat(fname,"/TMXXXXXX");
  1592.   unix_convert(fname,cnum,0,&bad_path);
  1593.   
  1594.   unixmode = unix_mode(cnum,createmode);
  1595.   
  1596.   fnum = find_free_file();
  1597.   if (fnum < 0)
  1598.     return(ERROR(ERRSRV,ERRnofids));
  1599.  
  1600.   if (!check_name(fname,cnum))
  1601.   {
  1602.     if((errno == ENOENT) && bad_path)
  1603.     {
  1604.       unix_ERR_class = ERRDOS;
  1605.       unix_ERR_code = ERRbadpath;
  1606.     }
  1607.     Files[fnum].reserved = False;
  1608.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1609.   }
  1610.  
  1611.   pstrcpy(fname2,(char *)mktemp(fname));
  1612.  
  1613.   /* Open file in dos compatibility share mode. */
  1614.   /* We should fail if file exists. */
  1615.   open_file_shared(fnum,cnum,fname2,(DENY_FCB<<4)|0xF, 0x10, unixmode, 
  1616.                    oplock_request, NULL, NULL);
  1617.  
  1618.   fsp = &Files[fnum];
  1619.  
  1620.   if (!fsp->open)
  1621.   {
  1622.     if((errno == ENOENT) && bad_path)
  1623.     {
  1624.       unix_ERR_class = ERRDOS;
  1625.       unix_ERR_code = ERRbadpath;
  1626.     }
  1627.     Files[fnum].reserved = False;
  1628.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1629.   }
  1630.  
  1631.   outsize = set_message(outbuf,1,2 + strlen(fname2),True);
  1632.   SSVAL(outbuf,smb_vwv0,fnum);
  1633.   CVAL(smb_buf(outbuf),0) = 4;
  1634.   pstrcpy(smb_buf(outbuf) + 1,fname2);
  1635.  
  1636.   if (oplock_request && lp_fake_oplocks(SNUM(cnum))) {
  1637.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1638.   }
  1639.   
  1640.   if(fsp->granted_oplock)
  1641.     CVAL(outbuf,smb_flg) |= CORE_OPLOCK_GRANTED;
  1642.  
  1643.   DEBUG(2,("created temp file %s\n",fname2));
  1644.   DEBUG(3,("%s ctemp %s fd=%d fnum=%d cnum=%d dmode=%d umode=%o\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum,createmode,unixmode));
  1645.   
  1646.   return(outsize);
  1647. }
  1648.  
  1649.  
  1650. /*******************************************************************
  1651. check if a user is allowed to delete a file
  1652. ********************************************************************/
  1653. static BOOL can_delete(char *fname,int cnum,int dirtype)
  1654. {
  1655.   struct stat sbuf;
  1656.   int fmode;
  1657.  
  1658.   if (!CAN_WRITE(cnum)) return(False);
  1659.  
  1660.   if (sys_lstat(fname,&sbuf) != 0) return(False);
  1661.   fmode = dos_mode(cnum,fname,&sbuf);
  1662.   if (fmode & aDIR) return(False);
  1663.   if (!lp_delete_readonly(SNUM(cnum))) {
  1664.     if (fmode & aRONLY) return(False);
  1665.   }
  1666.   if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM))
  1667.     return(False);
  1668.   if (!check_file_sharing(cnum,fname,False)) return(False);
  1669.   return(True);
  1670. }
  1671.  
  1672. /****************************************************************************
  1673.   reply to a unlink
  1674. ****************************************************************************/
  1675. int reply_unlink(char *inbuf,char *outbuf, int dum_size, int dum_bufsize)
  1676. {
  1677.   int outsize = 0;
  1678.   pstring name;
  1679.   int cnum;
  1680.   int dirtype;
  1681.   pstring directory;
  1682.   pstring mask;
  1683.   char *p;
  1684.   int count=0;
  1685.   int error = ERRnoaccess;
  1686.   BOOL has_wild;
  1687.   BOOL exists=False;
  1688.   BOOL bad_path = False;
  1689.  
  1690.   *directory = *mask = 0;
  1691.  
  1692.   cnum = SVAL(inbuf,smb_tid);
  1693.   dirtype = SVAL(inbuf,smb_vwv0);
  1694.   
  1695.   pstrcpy(name,smb_buf(inbuf) + 1);
  1696.    
  1697.   DEBUG(3,("reply_unlink : %s\n",name));
  1698.    
  1699.   unix_convert(name,cnum,0,&bad_path);
  1700.  
  1701.   p = strrchr(name,'/');
  1702.   if (!p) {
  1703.     pstrcpy(directory,"./");
  1704.     pstrcpy(mask,name);
  1705.   } else {
  1706.     *p = 0;
  1707.     pstrcpy(directory,name);
  1708.     pstrcpy(mask,p+1);
  1709.   }
  1710.  
  1711.   if (is_mangled(mask))
  1712.     check_mangled_stack(mask);
  1713.  
  1714.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  1715.  
  1716.   if (!has_wild) {
  1717.     pstrcat(directory,"/");
  1718.     pstrcat(directory,mask);
  1719.     if (can_delete(directory,cnum,dirtype) && !sys_unlink(directory)) count++;
  1720.     if (!count) exists = file_exist(directory,NULL);    
  1721.   } else {
  1722.     void *dirptr = NULL;
  1723.     char *dname;
  1724.  
  1725.     if (check_name(directory,cnum))
  1726.       dirptr = OpenDir(cnum, directory, True);
  1727.  
  1728.     /* XXXX the CIFS spec says that if bit0 of the flags2 field is set then
  1729.        the pattern matches against the long name, otherwise the short name 
  1730.        We don't implement this yet XXXX
  1731.        */
  1732.  
  1733.     if (dirptr)
  1734.       {
  1735.     error = ERRbadfile;
  1736.  
  1737.     if (strequal(mask,"????????.???"))
  1738.       pstrcpy(mask,"*");
  1739.  
  1740.     while ((dname = ReadDirName(dirptr)))
  1741.       {
  1742.         pstring fname;
  1743.         pstrcpy(fname,dname);
  1744.         
  1745.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  1746.  
  1747.         error = ERRnoaccess;
  1748.         slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
  1749.         if (!can_delete(fname,cnum,dirtype)) continue;
  1750.         if (!sys_unlink(fname)) count++;
  1751.         DEBUG(3,("reply_unlink : doing unlink on %s\n",fname));
  1752.       }
  1753.     CloseDir(dirptr);
  1754.       }
  1755.   }
  1756.   
  1757.   if (count == 0) {
  1758.     if (exists)
  1759.       return(ERROR(ERRDOS,error));
  1760.     else
  1761.     {
  1762.       if((errno == ENOENT) && bad_path)
  1763.       {
  1764.         unix_ERR_class = ERRDOS;
  1765.         unix_ERR_code = ERRbadpath;
  1766.       }
  1767.       return(UNIXERROR(ERRDOS,error));
  1768.     }
  1769.   }
  1770.   
  1771.   outsize = set_message(outbuf,0,0,True);
  1772.   
  1773.   return(outsize);
  1774. }
  1775.  
  1776.  
  1777. /****************************************************************************
  1778.    reply to a readbraw (core+ protocol)
  1779. ****************************************************************************/
  1780. int reply_readbraw(char *inbuf, char *outbuf, int dum_size, int dum_buffsize)
  1781. {
  1782.   int cnum,maxcount,mincount,fnum;
  1783.   int nread = 0;
  1784.   uint32 startpos;
  1785.   char *header = outbuf;
  1786.   int ret=0;
  1787.   int fd;
  1788.   char *fname;
  1789.  
  1790.   /*
  1791.    * Special check if an oplock break has been issued
  1792.    * and the readraw request croses on the wire, we must
  1793.    * return a zero length response here.
  1794.    */
  1795.  
  1796.   if(global_oplock_break)
  1797.   {
  1798.     _smb_setlen(header,0);
  1799.     transfer_file(0,Client,0,header,4,0);
  1800.     DEBUG(5,("readbraw - oplock break finished\n"));
  1801.     return -1;
  1802.   }
  1803.  
  1804.   cnum = SVAL(inbuf,smb_tid);
  1805.   fnum = GETFNUM(inbuf,smb_vwv0);
  1806.  
  1807.   startpos = IVAL(inbuf,smb_vwv1);
  1808.   maxcount = SVAL(inbuf,smb_vwv3);
  1809.   mincount = SVAL(inbuf,smb_vwv4);
  1810.  
  1811.   /* ensure we don't overrun the packet size */
  1812.   maxcount = MIN(65535,maxcount);
  1813.   maxcount = MAX(mincount,maxcount);
  1814.  
  1815.   if (!FNUM_OK(fnum,cnum) || !Files[fnum].can_read)
  1816.     {
  1817.       DEBUG(3,("fnum %d not open in readbraw - cache prime?\n",fnum));
  1818.       _smb_setlen(header,0);
  1819.       transfer_file(0,Client,0,header,4,0);
  1820.       return(-1);
  1821.     }
  1822.   else
  1823.     {
  1824.       fd = Files[fnum].fd_ptr->fd;
  1825.       fname = Files[fnum].name;
  1826.     }
  1827.  
  1828.  
  1829.   if (!is_locked(fnum,cnum,maxcount,startpos, F_RDLCK))
  1830.     {
  1831.       int size = Files[fnum].size;
  1832.       int sizeneeded = startpos + maxcount;
  1833.         
  1834.       if (size < sizeneeded) {
  1835.     struct stat st;
  1836.     if (fstat(Files[fnum].fd_ptr->fd,&st) == 0)
  1837.       size = st.st_size;
  1838.     if (!Files[fnum].can_write) 
  1839.       Files[fnum].size = size;
  1840.       }
  1841.  
  1842.       nread = MIN(maxcount,(int)(size - startpos));      
  1843.     }
  1844.  
  1845.   if (nread < mincount)
  1846.     nread = 0;
  1847.   
  1848.   DEBUG(3,("%s readbraw fnum=%d cnum=%d start=%d max=%d min=%d nread=%d\n",
  1849.        timestring(),
  1850.        fnum,cnum,startpos,
  1851.        maxcount,mincount,nread));
  1852.   
  1853. #if UNSAFE_READRAW
  1854.   {
  1855.     int predict=0;
  1856.     _smb_setlen(header,nread);
  1857.  
  1858. #if USE_READ_PREDICTION
  1859.     if (!Files[fnum].can_write)
  1860.       predict = read_predict(fd,startpos,header+4,NULL,nread);
  1861. #endif
  1862.  
  1863.     if ((nread-predict) > 0)
  1864.       seek_file(fnum,startpos + predict);
  1865.     
  1866.     ret = transfer_file(fd,Client,nread-predict,header,4+predict,
  1867.             startpos+predict);
  1868.   }
  1869.  
  1870.   if (ret != nread+4)
  1871.     DEBUG(0,("ERROR: file read failure on %s at %d for %d bytes (%d)\n",
  1872.          fname,startpos,nread,ret));
  1873.  
  1874. #else
  1875.   ret = read_file(fnum,header+4,startpos,nread);
  1876.   if (ret < mincount) ret = 0;
  1877.  
  1878.   _smb_setlen(header,ret);
  1879.   transfer_file(0,Client,0,header,4+ret,0);
  1880. #endif
  1881.  
  1882.   DEBUG(5,("readbraw finished\n"));
  1883.   return -1;
  1884. }
  1885.  
  1886.  
  1887. /****************************************************************************
  1888.   reply to a lockread (core+ protocol)
  1889. ****************************************************************************/
  1890. int reply_lockread(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1891. {
  1892.   int cnum,fnum;
  1893.   int nread = -1;
  1894.   char *data;
  1895.   int outsize = 0;
  1896.   uint32 startpos, numtoread;
  1897.   int eclass;
  1898.   uint32 ecode;
  1899.   
  1900.   cnum = SVAL(inbuf,smb_tid);
  1901.   fnum = GETFNUM(inbuf,smb_vwv0);
  1902.  
  1903.   CHECK_FNUM(fnum,cnum);
  1904.   CHECK_READ(fnum);
  1905.   CHECK_ERROR(fnum);
  1906.  
  1907.   numtoread = SVAL(inbuf,smb_vwv1);
  1908.   startpos = IVAL(inbuf,smb_vwv2);
  1909.   
  1910.   outsize = set_message(outbuf,5,3,True);
  1911.   numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
  1912.   data = smb_buf(outbuf) + 3;
  1913.   
  1914.   if(!do_lock( fnum, cnum, numtoread, startpos, F_RDLCK, &eclass, &ecode))
  1915.     return (ERROR(eclass,ecode));
  1916.  
  1917.   nread = read_file(fnum,data,startpos,numtoread);
  1918.   
  1919.   if (nread < 0)
  1920.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1921.   
  1922.   outsize += nread;
  1923.   SSVAL(outbuf,smb_vwv0,nread);
  1924.   SSVAL(outbuf,smb_vwv5,nread+3);
  1925.   SSVAL(smb_buf(outbuf),1,nread);
  1926.   
  1927.   DEBUG(3,("%s lockread fnum=%d cnum=%d num=%d nread=%d\n",timestring(),fnum,cnum,numtoread,nread));
  1928.   
  1929.   return(outsize);
  1930. }
  1931.  
  1932.  
  1933. /****************************************************************************
  1934.   reply to a read
  1935. ****************************************************************************/
  1936. int reply_read(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  1937. {
  1938.   int cnum,numtoread,fnum;
  1939.   int nread = 0;
  1940.   char *data;
  1941.   uint32 startpos;
  1942.   int outsize = 0;
  1943.   
  1944.   cnum = SVAL(inbuf,smb_tid);
  1945.   fnum = GETFNUM(inbuf,smb_vwv0);
  1946.  
  1947.   CHECK_FNUM(fnum,cnum);
  1948.   CHECK_READ(fnum);
  1949.   CHECK_ERROR(fnum);
  1950.  
  1951.   numtoread = SVAL(inbuf,smb_vwv1);
  1952.   startpos = IVAL(inbuf,smb_vwv2);
  1953.   
  1954.   outsize = set_message(outbuf,5,3,True);
  1955.   numtoread = MIN(BUFFER_SIZE-outsize,numtoread);
  1956.   data = smb_buf(outbuf) + 3;
  1957.   
  1958.   if (is_locked(fnum,cnum,numtoread,startpos,F_RDLCK))
  1959.     return(ERROR(ERRDOS,ERRlock));    
  1960.  
  1961.   if (numtoread > 0)
  1962.     nread = read_file(fnum,data,startpos,numtoread);
  1963.   
  1964.   if (nread < 0)
  1965.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  1966.   
  1967.   outsize += nread;
  1968.   SSVAL(outbuf,smb_vwv0,nread);
  1969.   SSVAL(outbuf,smb_vwv5,nread+3);
  1970.   CVAL(smb_buf(outbuf),0) = 1;
  1971.   SSVAL(smb_buf(outbuf),1,nread);
  1972.   
  1973.   DEBUG(3,("%s read fnum=%d cnum=%d num=%d nread=%d\n",timestring(),fnum,cnum,numtoread,nread));
  1974.   
  1975.   return(outsize);
  1976. }
  1977.  
  1978.  
  1979. /****************************************************************************
  1980.   reply to a read and X
  1981. ****************************************************************************/
  1982. int reply_read_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  1983. {
  1984.   int fnum = GETFNUM(inbuf,smb_vwv2);
  1985.   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
  1986.   int smb_maxcnt = SVAL(inbuf,smb_vwv5);
  1987.   int smb_mincnt = SVAL(inbuf,smb_vwv6);
  1988.   int cnum;
  1989.   int nread = -1;
  1990.   char *data;
  1991.   BOOL ok = False;
  1992.  
  1993.   cnum = SVAL(inbuf,smb_tid);
  1994.  
  1995.   CHECK_FNUM(fnum,cnum);
  1996.   CHECK_READ(fnum);
  1997.   CHECK_ERROR(fnum);
  1998.  
  1999.   set_message(outbuf,12,0,True);
  2000.   data = smb_buf(outbuf);
  2001.  
  2002.   if (is_locked(fnum,cnum,smb_maxcnt,smb_offs,F_RDLCK))
  2003.     return(ERROR(ERRDOS,ERRlock));
  2004.   nread = read_file(fnum,data,smb_offs,smb_maxcnt);
  2005.   ok = True;
  2006.   
  2007.   if (nread < 0)
  2008.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2009.   
  2010.   SSVAL(outbuf,smb_vwv5,nread);
  2011.   SSVAL(outbuf,smb_vwv6,smb_offset(data,outbuf));
  2012.   SSVAL(smb_buf(outbuf),-2,nread);
  2013.   
  2014.   DEBUG(3,("%s readX fnum=%d cnum=%d min=%d max=%d nread=%d\n",
  2015.     timestring(),fnum,cnum,
  2016.     smb_mincnt,smb_maxcnt,nread));
  2017.  
  2018.   chain_fnum = fnum;
  2019.  
  2020.   return chain_reply(inbuf,outbuf,length,bufsize);
  2021. }
  2022.  
  2023.  
  2024. /****************************************************************************
  2025.   reply to a writebraw (core+ or LANMAN1.0 protocol)
  2026. ****************************************************************************/
  2027. int reply_writebraw(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  2028. {
  2029.   int nwritten=0;
  2030.   int total_written=0;
  2031.   int numtowrite=0;
  2032.   int cnum,fnum;
  2033.   int outsize = 0;
  2034.   long startpos;
  2035.   char *data=NULL;
  2036.   BOOL write_through;
  2037.   int tcount;
  2038.  
  2039.   cnum = SVAL(inbuf,smb_tid);
  2040.   fnum = GETFNUM(inbuf,smb_vwv0);
  2041.  
  2042.   CHECK_FNUM(fnum,cnum);
  2043.   CHECK_WRITE(fnum);
  2044.   CHECK_ERROR(fnum);
  2045.   
  2046.   tcount = IVAL(inbuf,smb_vwv1);
  2047.   startpos = IVAL(inbuf,smb_vwv3);
  2048.   write_through = BITSETW(inbuf+smb_vwv7,0);
  2049.  
  2050.   /* We have to deal with slightly different formats depending
  2051.      on whether we are using the core+ or lanman1.0 protocol */
  2052.   if(Protocol <= PROTOCOL_COREPLUS) {
  2053.     numtowrite = SVAL(smb_buf(inbuf),-2);
  2054.     data = smb_buf(inbuf);
  2055.   } else {
  2056.     numtowrite = SVAL(inbuf,smb_vwv10);
  2057.     data = smb_base(inbuf) + SVAL(inbuf, smb_vwv11);
  2058.   }
  2059.  
  2060.   /* force the error type */
  2061.   CVAL(inbuf,smb_com) = SMBwritec;
  2062.   CVAL(outbuf,smb_com) = SMBwritec;
  2063.  
  2064.   if (is_locked(fnum,cnum,tcount,startpos,F_WRLCK))
  2065.     return(ERROR(ERRDOS,ERRlock));
  2066.  
  2067.   if (seek_file(fnum,startpos) != startpos)
  2068.     DEBUG(0,("couldn't seek to %d in writebraw\n",startpos));
  2069.  
  2070.   if (numtowrite>0)
  2071.     nwritten = write_file(fnum,data,numtowrite);
  2072.   
  2073.   DEBUG(3,("%s writebraw1 fnum=%d cnum=%d start=%d num=%d wrote=%d sync=%d\n",
  2074.        timestring(),fnum,cnum,startpos,numtowrite,nwritten,write_through));
  2075.  
  2076.   if (nwritten < numtowrite) 
  2077.     return(UNIXERROR(ERRHRD,ERRdiskfull));
  2078.  
  2079.   total_written = nwritten;
  2080.  
  2081.   /* Return a message to the redirector to tell it
  2082.      to send more bytes */
  2083.   CVAL(outbuf,smb_com) = SMBwritebraw;
  2084.   SSVALS(outbuf,smb_vwv0,-1);
  2085.   outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
  2086.   send_smb(Client,outbuf);
  2087.   
  2088.   /* Now read the raw data into the buffer and write it */
  2089.   if (read_smb_length(Client,inbuf,SMB_SECONDARY_WAIT) == -1) {
  2090.     exit_server("secondary writebraw failed");
  2091.   }
  2092.   
  2093.   /* Even though this is not an smb message, smb_len
  2094.      returns the generic length of an smb message */
  2095.   numtowrite = smb_len(inbuf);
  2096.  
  2097.   if (tcount > nwritten+numtowrite) {
  2098.     DEBUG(3,("Client overestimated the write %d %d %d\n",
  2099.          tcount,nwritten,numtowrite));
  2100.   }
  2101.  
  2102.   nwritten = transfer_file(Client,Files[fnum].fd_ptr->fd,numtowrite,NULL,0,
  2103.                startpos+nwritten);
  2104.   total_written += nwritten;
  2105.   
  2106.   /* Set up outbuf to return the correct return */
  2107.   outsize = set_message(outbuf,1,0,True);
  2108.   CVAL(outbuf,smb_com) = SMBwritec;
  2109.   SSVAL(outbuf,smb_vwv0,total_written);
  2110.  
  2111.   if (nwritten < numtowrite) {
  2112.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2113.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2114.   }
  2115.  
  2116.   if (lp_syncalways(SNUM(cnum)) || write_through)
  2117.     sync_file(cnum,fnum);
  2118.  
  2119.   DEBUG(3,("%s writebraw2 fnum=%d cnum=%d start=%d num=%d wrote=%d\n",
  2120.        timestring(),fnum,cnum,startpos,numtowrite,total_written));
  2121.  
  2122.   /* we won't return a status if write through is not selected - this 
  2123.      follows what WfWg does */
  2124.   if (!write_through && total_written==tcount)
  2125.     return(-1);
  2126.  
  2127.   return(outsize);
  2128. }
  2129.  
  2130.  
  2131. /****************************************************************************
  2132.   reply to a writeunlock (core+)
  2133. ****************************************************************************/
  2134. int reply_writeunlock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2135. {
  2136.   int cnum,fnum;
  2137.   int nwritten = -1;
  2138.   int outsize = 0;
  2139.   char *data;
  2140.   uint32 numtowrite,startpos;
  2141.   int eclass;
  2142.   uint32 ecode;
  2143.   
  2144.   cnum = SVAL(inbuf,smb_tid);
  2145.   fnum = GETFNUM(inbuf,smb_vwv0);
  2146.  
  2147.   CHECK_FNUM(fnum,cnum);
  2148.   CHECK_WRITE(fnum);
  2149.   CHECK_ERROR(fnum);
  2150.  
  2151.   numtowrite = SVAL(inbuf,smb_vwv1);
  2152.   startpos = IVAL(inbuf,smb_vwv2);
  2153.   data = smb_buf(inbuf) + 3;
  2154.   
  2155.   if (is_locked(fnum,cnum,numtowrite,startpos,F_WRLCK))
  2156.     return(ERROR(ERRDOS,ERRlock));
  2157.  
  2158.   seek_file(fnum,startpos);
  2159.  
  2160.   /* The special X/Open SMB protocol handling of
  2161.      zero length writes is *NOT* done for
  2162.      this call */
  2163.   if(numtowrite == 0)
  2164.     nwritten = 0;
  2165.   else
  2166.     nwritten = write_file(fnum,data,numtowrite);
  2167.   
  2168.   if (lp_syncalways(SNUM(cnum)))
  2169.     sync_file(cnum,fnum);
  2170.  
  2171.   if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0))
  2172.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2173.  
  2174.   if(!do_unlock(fnum, cnum, numtowrite, startpos, &eclass, &ecode))
  2175.     return(ERROR(eclass,ecode));
  2176.  
  2177.   outsize = set_message(outbuf,1,0,True);
  2178.   
  2179.   SSVAL(outbuf,smb_vwv0,nwritten);
  2180.   
  2181.   DEBUG(3,("%s writeunlock fnum=%d cnum=%d num=%d wrote=%d\n",
  2182.        timestring(),fnum,cnum,numtowrite,nwritten));
  2183.   
  2184.   return(outsize);
  2185. }
  2186.  
  2187.  
  2188. /****************************************************************************
  2189.   reply to a write
  2190. ****************************************************************************/
  2191. int reply_write(char *inbuf,char *outbuf,int dum1,int dum2)
  2192. {
  2193.   int cnum,numtowrite,fnum;
  2194.   int nwritten = -1;
  2195.   int outsize = 0;
  2196.   int startpos;
  2197.   char *data;
  2198.  
  2199.   dum1 = dum2 = 0;
  2200.  
  2201.   
  2202.   cnum = SVAL(inbuf,smb_tid);
  2203.   fnum = GETFNUM(inbuf,smb_vwv0);
  2204.  
  2205.   CHECK_FNUM(fnum,cnum);
  2206.   CHECK_WRITE(fnum);
  2207.   CHECK_ERROR(fnum);
  2208.  
  2209.   numtowrite = SVAL(inbuf,smb_vwv1);
  2210.   startpos = IVAL(inbuf,smb_vwv2);
  2211.   data = smb_buf(inbuf) + 3;
  2212.   
  2213.   if (is_locked(fnum,cnum,numtowrite,startpos,F_WRLCK))
  2214.     return(ERROR(ERRDOS,ERRlock));
  2215.  
  2216.   seek_file(fnum,startpos);
  2217.  
  2218.   /* X/Open SMB protocol says that if smb_vwv1 is
  2219.      zero then the file size should be extended or
  2220.      truncated to the size given in smb_vwv[2-3] */
  2221.   if(numtowrite == 0)
  2222.     nwritten = set_filelen(Files[fnum].fd_ptr->fd, startpos);
  2223.   else
  2224.     nwritten = write_file(fnum,data,numtowrite);
  2225.   
  2226.   if (lp_syncalways(SNUM(cnum)))
  2227.     sync_file(cnum,fnum);
  2228.  
  2229.   if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0))
  2230.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2231.  
  2232.   outsize = set_message(outbuf,1,0,True);
  2233.   
  2234.   SSVAL(outbuf,smb_vwv0,nwritten);
  2235.  
  2236.   if (nwritten < numtowrite) {
  2237.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2238.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2239.   }
  2240.   
  2241.   DEBUG(3,("%s write fnum=%d cnum=%d num=%d wrote=%d\n",timestring(),fnum,cnum,numtowrite,nwritten));
  2242.   
  2243.   return(outsize);
  2244. }
  2245.  
  2246.  
  2247. /****************************************************************************
  2248.   reply to a write and X
  2249. ****************************************************************************/
  2250. int reply_write_and_X(char *inbuf,char *outbuf,int length,int bufsize)
  2251. {
  2252.   int fnum = GETFNUM(inbuf,smb_vwv2);
  2253.   uint32 smb_offs = IVAL(inbuf,smb_vwv3);
  2254.   int smb_dsize = SVAL(inbuf,smb_vwv10);
  2255.   int smb_doff = SVAL(inbuf,smb_vwv11);
  2256.   BOOL write_through = BITSETW(inbuf+smb_vwv7,0);
  2257.   int cnum;
  2258.   int nwritten = -1;
  2259.   char *data;
  2260.  
  2261.   cnum = SVAL(inbuf,smb_tid);
  2262.  
  2263.   CHECK_FNUM(fnum,cnum);
  2264.   CHECK_WRITE(fnum);
  2265.   CHECK_ERROR(fnum);
  2266.  
  2267.   data = smb_base(inbuf) + smb_doff;
  2268.  
  2269.   if (is_locked(fnum,cnum,smb_dsize,smb_offs,F_WRLCK))
  2270.     return(ERROR(ERRDOS,ERRlock));
  2271.  
  2272.   seek_file(fnum,smb_offs);
  2273.   
  2274.   /* X/Open SMB protocol says that, unlike SMBwrite
  2275.      if the length is zero then NO truncation is
  2276.      done, just a write of zero. To truncate a file,
  2277.      use SMBwrite. */
  2278.   if(smb_dsize == 0)
  2279.     nwritten = 0;
  2280.   else
  2281.     nwritten = write_file(fnum,data,smb_dsize);
  2282.   
  2283.   if(((nwritten == 0) && (smb_dsize != 0))||(nwritten < 0))
  2284.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2285.  
  2286.   set_message(outbuf,6,0,True);
  2287.   
  2288.   SSVAL(outbuf,smb_vwv2,nwritten);
  2289.   
  2290.   if (nwritten < smb_dsize) {
  2291.     CVAL(outbuf,smb_rcls) = ERRHRD;
  2292.     SSVAL(outbuf,smb_err,ERRdiskfull);      
  2293.   }
  2294.  
  2295.   DEBUG(3,("%s writeX fnum=%d cnum=%d num=%d wrote=%d\n",timestring(),fnum,cnum,smb_dsize,nwritten));
  2296.  
  2297.   chain_fnum = fnum;
  2298.  
  2299.   if (lp_syncalways(SNUM(cnum)) || write_through)
  2300.     sync_file(cnum,fnum);
  2301.  
  2302.   return chain_reply(inbuf,outbuf,length,bufsize);
  2303. }
  2304.  
  2305.  
  2306. /****************************************************************************
  2307.   reply to a lseek
  2308. ****************************************************************************/
  2309. int reply_lseek(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2310. {
  2311.   int cnum,fnum;
  2312.   uint32 startpos;
  2313.   int32 res= -1;
  2314.   int mode,umode;
  2315.   int outsize = 0;
  2316.   
  2317.   cnum = SVAL(inbuf,smb_tid);
  2318.   fnum = GETFNUM(inbuf,smb_vwv0);
  2319.  
  2320.   CHECK_FNUM(fnum,cnum);
  2321.   CHECK_ERROR(fnum);
  2322.  
  2323.   mode = SVAL(inbuf,smb_vwv1) & 3;
  2324.   startpos = IVAL(inbuf,smb_vwv2);
  2325.  
  2326.   switch (mode & 3) 
  2327.     {
  2328.     case 0: umode = SEEK_SET; break;
  2329.     case 1: umode = SEEK_CUR; break;
  2330.     case 2: umode = SEEK_END; break;
  2331.     default:
  2332.       umode = SEEK_SET; break;
  2333.     }
  2334.   
  2335.   res = lseek(Files[fnum].fd_ptr->fd,startpos,umode);
  2336.   Files[fnum].pos = res;
  2337.   
  2338.   outsize = set_message(outbuf,2,0,True);
  2339.   SIVALS(outbuf,smb_vwv0,res);
  2340.   
  2341.   DEBUG(3,("%s lseek fnum=%d cnum=%d ofs=%d mode=%d\n",timestring(),fnum,cnum,startpos,mode));
  2342.   
  2343.   return(outsize);
  2344. }
  2345.  
  2346.  
  2347. /****************************************************************************
  2348.   reply to a flush
  2349. ****************************************************************************/
  2350. int reply_flush(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2351. {
  2352.   int cnum, fnum;
  2353.   int outsize = set_message(outbuf,0,0,True);
  2354.  
  2355.   cnum = SVAL(inbuf,smb_tid);
  2356.   fnum = GETFNUM(inbuf,smb_vwv0);
  2357.  
  2358.   if (fnum != 0xFFFF) {
  2359.     CHECK_FNUM(fnum,cnum);
  2360.     CHECK_ERROR(fnum);
  2361.   }
  2362.  
  2363.   if (fnum == 0xFFFF)
  2364.     {
  2365.       int i;
  2366.       for (i=0;i<MAX_OPEN_FILES;i++)
  2367.     if (OPEN_FNUM(i))
  2368.       sync_file(cnum,i);
  2369.     }
  2370.   else
  2371.     sync_file(cnum,fnum);
  2372.  
  2373.   DEBUG(3,("%s flush fnum=%d\n",timestring(),fnum));
  2374.   return(outsize);
  2375. }
  2376.  
  2377.  
  2378. /****************************************************************************
  2379.   reply to a exit
  2380. ****************************************************************************/
  2381. int reply_exit(char *inbuf,char *outbuf, int size, int bufsize)
  2382. {
  2383.   int outsize = set_message(outbuf,0,0,True);
  2384.   DEBUG(3,("%s exit\n",timestring()));
  2385.   
  2386.   return(outsize);
  2387. }
  2388.  
  2389.  
  2390. /****************************************************************************
  2391.   reply to a close
  2392. ****************************************************************************/
  2393. int reply_close(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2394. {
  2395.   int fnum,cnum;
  2396.   int outsize = 0;
  2397.   time_t mtime;
  2398.   int32 eclass = 0, err = 0;
  2399.  
  2400.   outsize = set_message(outbuf,0,0,True);
  2401.  
  2402.   cnum = SVAL(inbuf,smb_tid);
  2403.  
  2404.   /* If it's an IPC, pass off to the pipe handler. */
  2405.   if (IS_IPC(cnum))
  2406.     return reply_pipe_close(inbuf,outbuf);
  2407.  
  2408.   fnum = GETFNUM(inbuf,smb_vwv0);
  2409.  
  2410.   CHECK_FNUM(fnum,cnum);
  2411.  
  2412.   if(HAS_CACHED_ERROR(fnum)) {
  2413.     eclass = Files[fnum].wbmpx_ptr->wr_errclass;
  2414.     err = Files[fnum].wbmpx_ptr->wr_error;
  2415.   }
  2416.  
  2417.   mtime = make_unix_date3(inbuf+smb_vwv1);
  2418.  
  2419.   /* try and set the date */
  2420.   set_filetime(cnum, Files[fnum].name,mtime);
  2421.  
  2422.   DEBUG(3,("%s close fd=%d fnum=%d cnum=%d (numopen=%d)\n",
  2423.        timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,
  2424.        Connections[cnum].num_files_open));
  2425.   
  2426.   close_file(fnum,True);
  2427.  
  2428.   /* We have a cached error */
  2429.   if(eclass || err)
  2430.     return(ERROR(eclass,err));
  2431.  
  2432.   return(outsize);
  2433. }
  2434.  
  2435.  
  2436. /****************************************************************************
  2437.   reply to a writeclose (Core+ protocol)
  2438. ****************************************************************************/
  2439. int reply_writeclose(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2440. {
  2441.   int cnum,numtowrite,fnum;
  2442.   int nwritten = -1;
  2443.   int outsize = 0;
  2444.   int startpos;
  2445.   char *data;
  2446.   time_t mtime;
  2447.   
  2448.   cnum = SVAL(inbuf,smb_tid);
  2449.   fnum = GETFNUM(inbuf,smb_vwv0);
  2450.  
  2451.   CHECK_FNUM(fnum,cnum);
  2452.   CHECK_WRITE(fnum);
  2453.   CHECK_ERROR(fnum);
  2454.  
  2455.   numtowrite = SVAL(inbuf,smb_vwv1);
  2456.   startpos = IVAL(inbuf,smb_vwv2);
  2457.   mtime = make_unix_date3(inbuf+smb_vwv4);
  2458.   data = smb_buf(inbuf) + 1;
  2459.   
  2460.   if (is_locked(fnum,cnum,numtowrite,startpos,F_WRLCK))
  2461.     return(ERROR(ERRDOS,ERRlock));
  2462.       
  2463.   seek_file(fnum,startpos);
  2464.       
  2465.   nwritten = write_file(fnum,data,numtowrite);
  2466.  
  2467.   set_filetime(cnum, Files[fnum].name,mtime);
  2468.   
  2469.   DEBUG(3,("%s writeclose fnum=%d cnum=%d num=%d wrote=%d (numopen=%d)\n",
  2470.        timestring(),fnum,cnum,numtowrite,nwritten,
  2471.        Connections[cnum].num_files_open));
  2472.   
  2473.   close_file(fnum,True);
  2474.  
  2475.   if (nwritten <= 0)
  2476.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2477.   
  2478.   outsize = set_message(outbuf,1,0,True);
  2479.   
  2480.   SSVAL(outbuf,smb_vwv0,nwritten);
  2481.   return(outsize);
  2482. }
  2483.  
  2484.  
  2485. /****************************************************************************
  2486.   reply to a lock
  2487. ****************************************************************************/
  2488. int reply_lock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2489. {
  2490.   int fnum,cnum;
  2491.   int outsize = set_message(outbuf,0,0,True);
  2492.   uint32 count,offset;
  2493.   int eclass;
  2494.   uint32 ecode;
  2495.  
  2496.   cnum = SVAL(inbuf,smb_tid);
  2497.   fnum = GETFNUM(inbuf,smb_vwv0);
  2498.  
  2499.   CHECK_FNUM(fnum,cnum);
  2500.   CHECK_ERROR(fnum);
  2501.  
  2502.   count = IVAL(inbuf,smb_vwv1);
  2503.   offset = IVAL(inbuf,smb_vwv3);
  2504.  
  2505.   DEBUG(3,("%s lock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
  2506.  
  2507.   if(!do_lock( fnum, cnum, count, offset, F_WRLCK, &eclass, &ecode))
  2508.     return (ERROR(eclass,ecode));
  2509.   
  2510.   return(outsize);
  2511. }
  2512.  
  2513.  
  2514. /****************************************************************************
  2515.   reply to a unlock
  2516. ****************************************************************************/
  2517. int reply_unlock(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2518. {
  2519.   int fnum,cnum;
  2520.   int outsize = set_message(outbuf,0,0,True);
  2521.   uint32 count,offset;
  2522.   int eclass;
  2523.   uint32 ecode;
  2524.   
  2525.   cnum = SVAL(inbuf,smb_tid);
  2526.   fnum = GETFNUM(inbuf,smb_vwv0);
  2527.  
  2528.   CHECK_FNUM(fnum,cnum);
  2529.   CHECK_ERROR(fnum);
  2530.  
  2531.   count = IVAL(inbuf,smb_vwv1);
  2532.   offset = IVAL(inbuf,smb_vwv3);
  2533.  
  2534.   if(!do_unlock(fnum, cnum, count, offset, &eclass, &ecode))
  2535.     return (ERROR(eclass,ecode));
  2536.  
  2537.   DEBUG(3,("%s unlock fd=%d fnum=%d cnum=%d ofs=%d cnt=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum,offset,count));
  2538.   
  2539.   return(outsize);
  2540. }
  2541.  
  2542.  
  2543. /****************************************************************************
  2544.   reply to a tdis
  2545. ****************************************************************************/
  2546. int reply_tdis(char *inbuf,char *outbuf, int size, int bufsize)
  2547. {
  2548.   int cnum;
  2549.   int outsize = set_message(outbuf,0,0,True);
  2550.   uint16 vuid;
  2551.  
  2552.   cnum = SVAL(inbuf,smb_tid);
  2553.   vuid = SVAL(inbuf,smb_uid);
  2554.  
  2555.   if (!OPEN_CNUM(cnum)) {
  2556.     DEBUG(4,("Invalid cnum in tdis (%d)\n",cnum));
  2557.     return(ERROR(ERRSRV,ERRinvnid));
  2558.   }
  2559.  
  2560.   Connections[cnum].used = False;
  2561.  
  2562.   close_cnum(cnum,vuid);
  2563.   
  2564.   DEBUG(3,("%s tdis cnum=%d\n",timestring(),cnum));
  2565.  
  2566.   return outsize;
  2567. }
  2568.  
  2569.  
  2570.  
  2571. /****************************************************************************
  2572.   reply to a echo
  2573. ****************************************************************************/
  2574. int reply_echo(char *inbuf,char *outbuf, int size, int bufsize)
  2575. {
  2576.   int cnum;
  2577.   int smb_reverb = SVAL(inbuf,smb_vwv0);
  2578.   int seq_num;
  2579.   int data_len = smb_buflen(inbuf);
  2580.   int outsize = set_message(outbuf,1,data_len,True);
  2581.  
  2582.   cnum = SVAL(inbuf,smb_tid);
  2583.  
  2584.   /* According to the latest CIFS spec we shouldn't
  2585.      care what the TID is.
  2586.    */
  2587.  
  2588. #if 0
  2589.   if (cnum != 0xFFFF && !OPEN_CNUM(cnum))
  2590.     {
  2591.       DEBUG(4,("Invalid cnum in echo (%d)\n",cnum));
  2592.       return(ERROR(ERRSRV,ERRinvnid));
  2593.     }
  2594. #endif
  2595.  
  2596.   /* copy any incoming data back out */
  2597.   if (data_len > 0)
  2598.     memcpy(smb_buf(outbuf),smb_buf(inbuf),data_len);
  2599.  
  2600.   if (smb_reverb > 100)
  2601.     {
  2602.       DEBUG(0,("large reverb (%d)?? Setting to 100\n",smb_reverb));
  2603.       smb_reverb = 100;
  2604.     }
  2605.  
  2606.   for (seq_num =1 ; seq_num <= smb_reverb ; seq_num++)
  2607.     {
  2608.       SSVAL(outbuf,smb_vwv0,seq_num);
  2609.  
  2610.       smb_setlen(outbuf,outsize - 4);
  2611.  
  2612.       send_smb(Client,outbuf);
  2613.     }
  2614.  
  2615.   DEBUG(3,("%s echo %d times cnum=%d\n",timestring(),smb_reverb,cnum));
  2616.  
  2617.   return -1;
  2618. }
  2619.  
  2620.  
  2621. /****************************************************************************
  2622.   reply to a printopen
  2623. ****************************************************************************/
  2624. int reply_printopen(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2625. {
  2626.   pstring fname;
  2627.   pstring fname2;
  2628.   int cnum;
  2629.   int fnum = -1;
  2630.   int outsize = 0;
  2631.  
  2632.   *fname = *fname2 = 0;
  2633.  
  2634.   cnum = SVAL(inbuf,smb_tid);
  2635.  
  2636.   if (!CAN_PRINT(cnum))
  2637.     return(ERROR(ERRDOS,ERRnoaccess));
  2638.  
  2639.   {
  2640.     pstring s;
  2641.     char *p;
  2642.     pstrcpy(s,smb_buf(inbuf)+1);
  2643.     p = s;
  2644.     while (*p)
  2645.       {
  2646.     if (!(isalnum(*p) || strchr("._-",*p)))
  2647.       *p = 'X';
  2648.     p++;
  2649.       }
  2650.  
  2651.     if (strlen(s) > 10) s[10] = 0;
  2652.  
  2653.     slprintf(fname,sizeof(fname)-1, "%s.XXXXXX",s);  
  2654.   }
  2655.  
  2656.   fnum = find_free_file();
  2657.   if (fnum < 0)
  2658.     return(ERROR(ERRSRV,ERRnofids));
  2659.  
  2660.   pstrcpy(fname2,(char *)mktemp(fname));
  2661.  
  2662.   if (!check_name(fname2,cnum)) {
  2663.       Files[fnum].reserved = False;
  2664.       return(ERROR(ERRDOS,ERRnoaccess));
  2665.   }
  2666.  
  2667.   /* Open for exclusive use, write only. */
  2668.   open_file_shared(fnum,cnum,fname2,(DENY_ALL<<4)|1, 0x12, unix_mode(cnum,0), 
  2669.                    0, NULL, NULL);
  2670.  
  2671.   if (!Files[fnum].open) {
  2672.       Files[fnum].reserved = False;
  2673.       return(UNIXERROR(ERRDOS,ERRnoaccess));
  2674.   }
  2675.  
  2676.   /* force it to be a print file */
  2677.   Files[fnum].print_file = True;
  2678.   
  2679.   outsize = set_message(outbuf,1,0,True);
  2680.   SSVAL(outbuf,smb_vwv0,fnum);
  2681.   
  2682.   DEBUG(3,("%s openprint %s fd=%d fnum=%d cnum=%d\n",timestring(),fname2,Files[fnum].fd_ptr->fd,fnum,cnum));
  2683.   
  2684.   return(outsize);
  2685. }
  2686.  
  2687.  
  2688. /****************************************************************************
  2689.   reply to a printclose
  2690. ****************************************************************************/
  2691. int reply_printclose(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2692. {
  2693.   int fnum,cnum;
  2694.   int outsize = set_message(outbuf,0,0,True);
  2695.   
  2696.   cnum = SVAL(inbuf,smb_tid);
  2697.   fnum = GETFNUM(inbuf,smb_vwv0);
  2698.  
  2699.   CHECK_FNUM(fnum,cnum);
  2700.   CHECK_ERROR(fnum);
  2701.  
  2702.   if (!CAN_PRINT(cnum))
  2703.     return(ERROR(ERRDOS,ERRnoaccess));
  2704.   
  2705.   DEBUG(3,("%s printclose fd=%d fnum=%d cnum=%d\n",timestring(),Files[fnum].fd_ptr->fd,fnum,cnum));
  2706.   
  2707.   close_file(fnum,True);
  2708.   
  2709.   return(outsize);
  2710. }
  2711.  
  2712.  
  2713. /****************************************************************************
  2714.   reply to a printqueue
  2715. ****************************************************************************/
  2716. int reply_printqueue(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2717. {
  2718.   int cnum;
  2719.   int outsize = set_message(outbuf,2,3,True);
  2720.   int max_count = SVAL(inbuf,smb_vwv0);
  2721.   int start_index = SVAL(inbuf,smb_vwv1);
  2722.   uint16 vuid;
  2723.  
  2724.   cnum = SVAL(inbuf,smb_tid);
  2725.   vuid = SVAL(inbuf,smb_uid);
  2726.  
  2727. /* allow checking the queue for anyone */
  2728. #if 0
  2729.   if (!CAN_PRINT(cnum))
  2730.     return(ERROR(ERRDOS,ERRnoaccess));
  2731. #endif
  2732.  
  2733.   SSVAL(outbuf,smb_vwv0,0);
  2734.   SSVAL(outbuf,smb_vwv1,0);
  2735.   CVAL(smb_buf(outbuf),0) = 1;
  2736.   SSVAL(smb_buf(outbuf),1,0);
  2737.   
  2738.   DEBUG(3,("%s printqueue cnum=%d start_index=%d max_count=%d\n",
  2739.     timestring(),cnum,start_index,max_count));
  2740.  
  2741.   if (!OPEN_CNUM(cnum) || !Connections[cnum].printer)
  2742.     {
  2743.       int i;
  2744.       cnum = -1;
  2745.  
  2746.       for (i=0;i<MAX_CONNECTIONS;i++)
  2747.     if (CAN_PRINT(i) && Connections[i].printer)
  2748.       cnum = i;
  2749.  
  2750.       if (cnum == -1)
  2751.     for (i=0;i<MAX_CONNECTIONS;i++)
  2752.       if (OPEN_CNUM(i))
  2753.         cnum = i;
  2754.  
  2755.       if (!OPEN_CNUM(cnum))
  2756.     return(ERROR(ERRSRV,ERRinvnid));
  2757.  
  2758.       DEBUG(5,("connection not open or not a printer, using cnum %d\n",cnum));
  2759.     }
  2760.  
  2761.   if (!become_user(&Connections[cnum], cnum, vuid))
  2762.     return(ERROR(ERRSRV,ERRinvnid));
  2763.  
  2764.   {
  2765.     print_queue_struct *queue = NULL;
  2766.     char *p = smb_buf(outbuf) + 3;
  2767.     int count = get_printqueue(SNUM(cnum),cnum,&queue,NULL);
  2768.     int num_to_get = ABS(max_count);
  2769.     int first = (max_count>0?start_index:start_index+max_count+1);
  2770.     int i;
  2771.  
  2772.     if (first >= count)
  2773.       num_to_get = 0;
  2774.     else
  2775.       num_to_get = MIN(num_to_get,count-first);
  2776.     
  2777.  
  2778.     for (i=first;i<first+num_to_get;i++)
  2779.       {
  2780.     put_dos_date2(p,0,queue[i].time);
  2781.     CVAL(p,4) = (queue[i].status==LPQ_PRINTING?2:3);
  2782.     SSVAL(p,5,printjob_encode(SNUM(cnum), queue[i].job));
  2783.     SIVAL(p,7,queue[i].size);
  2784.     CVAL(p,11) = 0;
  2785.     StrnCpy(p+12,queue[i].user,16);
  2786.     p += 28;
  2787.       }
  2788.  
  2789.     if (count > 0)
  2790.       {
  2791.     outsize = set_message(outbuf,2,28*count+3,False);      
  2792.     SSVAL(outbuf,smb_vwv0,count);
  2793.     SSVAL(outbuf,smb_vwv1,(max_count>0?first+count:first-1));
  2794.     CVAL(smb_buf(outbuf),0) = 1;
  2795.     SSVAL(smb_buf(outbuf),1,28*count);
  2796.       }
  2797.  
  2798.     if (queue) free(queue);
  2799.       
  2800.     DEBUG(3,("%d entries returned in queue\n",count));
  2801.   }
  2802.   
  2803.   return(outsize);
  2804. }
  2805.  
  2806.  
  2807. /****************************************************************************
  2808.   reply to a printwrite
  2809. ****************************************************************************/
  2810. int reply_printwrite(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2811. {
  2812.   int cnum,numtowrite,fnum;
  2813.   int outsize = set_message(outbuf,0,0,True);
  2814.   char *data;
  2815.   
  2816.   cnum = SVAL(inbuf,smb_tid);
  2817.  
  2818.   if (!CAN_PRINT(cnum))
  2819.     return(ERROR(ERRDOS,ERRnoaccess));
  2820.  
  2821.   fnum = GETFNUM(inbuf,smb_vwv0);
  2822.  
  2823.   CHECK_FNUM(fnum,cnum);
  2824.   CHECK_WRITE(fnum);
  2825.   CHECK_ERROR(fnum);
  2826.  
  2827.   numtowrite = SVAL(smb_buf(inbuf),1);
  2828.   data = smb_buf(inbuf) + 3;
  2829.   
  2830.   if (write_file(fnum,data,numtowrite) != numtowrite)
  2831.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2832.   
  2833.   DEBUG(3,("%s printwrite fnum=%d cnum=%d num=%d\n",timestring(),fnum,cnum,numtowrite));
  2834.   
  2835.   return(outsize);
  2836. }
  2837.  
  2838.  
  2839. /****************************************************************************
  2840.   reply to a mkdir
  2841. ****************************************************************************/
  2842. int reply_mkdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2843. {
  2844.   pstring directory;
  2845.   int cnum;
  2846.   int outsize,ret= -1;
  2847.   BOOL bad_path = False;
  2848.  
  2849.   pstrcpy(directory,smb_buf(inbuf) + 1);
  2850.   cnum = SVAL(inbuf,smb_tid);
  2851.   unix_convert(directory,cnum,0,&bad_path);
  2852.   
  2853.   if (check_name(directory,cnum))
  2854.     ret = sys_mkdir(directory,unix_mode(cnum,aDIR));
  2855.   
  2856.   if (ret < 0)
  2857.   {
  2858.     if((errno == ENOENT) && bad_path)
  2859.     {
  2860.       unix_ERR_class = ERRDOS;
  2861.       unix_ERR_code = ERRbadpath;
  2862.     }
  2863.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  2864.   }
  2865.  
  2866.   outsize = set_message(outbuf,0,0,True);
  2867.   
  2868.   DEBUG(3,("%s mkdir %s cnum=%d ret=%d\n",timestring(),directory,cnum,ret));
  2869.   
  2870.   return(outsize);
  2871. }
  2872.  
  2873. /****************************************************************************
  2874. Static function used by reply_rmdir to delete an entire directory
  2875. tree recursively.
  2876. ****************************************************************************/
  2877. static BOOL recursive_rmdir(char *directory)
  2878. {
  2879.   char *dname = NULL;
  2880.   BOOL ret = False;
  2881.   void *dirptr = OpenDir(-1, directory, False);
  2882.  
  2883.   if(dirptr == NULL)
  2884.     return True;
  2885.  
  2886.   while((dname = ReadDirName(dirptr)))
  2887.   {
  2888.     pstring fullname;
  2889.     struct stat st;
  2890.  
  2891.     if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2892.       continue;
  2893.  
  2894.     /* Construct the full name. */
  2895.     if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname))
  2896.     {
  2897.       errno = ENOMEM;
  2898.       ret = True;
  2899.       break;
  2900.     }
  2901.     pstrcpy(fullname, directory);
  2902.     pstrcat(fullname, "/");
  2903.     pstrcat(fullname, dname);
  2904.  
  2905.     if(sys_lstat(fullname, &st) != 0)
  2906.     {
  2907.       ret = True;
  2908.       break;
  2909.     }
  2910.  
  2911.     if(st.st_mode & S_IFDIR)
  2912.     {
  2913.       if(recursive_rmdir(fullname)!=0)
  2914.       {
  2915.         ret = True;
  2916.         break;
  2917.       }
  2918.       if(sys_rmdir(fullname) != 0)
  2919.       {
  2920.         ret = True;
  2921.         break;
  2922.       }
  2923.     }
  2924.     else if(sys_unlink(fullname) != 0)
  2925.     {
  2926.       ret = True;
  2927.       break;
  2928.     }
  2929.   }
  2930.   CloseDir(dirptr);
  2931.   return ret;
  2932. }
  2933.  
  2934. /****************************************************************************
  2935.   reply to a rmdir
  2936. ****************************************************************************/
  2937. int reply_rmdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  2938. {
  2939.   pstring directory;
  2940.   int cnum;
  2941.   int outsize = 0;
  2942.   BOOL ok = False;
  2943.   BOOL bad_path = False;
  2944.  
  2945.   cnum = SVAL(inbuf,smb_tid);
  2946.   pstrcpy(directory,smb_buf(inbuf) + 1);
  2947.   unix_convert(directory,cnum,0,&bad_path);
  2948.   
  2949.   if (check_name(directory,cnum))
  2950.     {
  2951.  
  2952.       dptr_closepath(directory,SVAL(inbuf,smb_pid));
  2953.       ok = (sys_rmdir(directory) == 0);
  2954.       if(!ok && (errno == ENOTEMPTY) && lp_veto_files(SNUM(cnum)))
  2955.         {
  2956.           /* Check to see if the only thing in this directory are
  2957.              vetoed files/directories. If so then delete them and
  2958.              retry. If we fail to delete any of them (and we *don't*
  2959.              do a recursive delete) then fail the rmdir. */
  2960.           BOOL all_veto_files = True;
  2961.           char *dname;
  2962.           void *dirptr = OpenDir(cnum, directory, False);
  2963.  
  2964.           if(dirptr != NULL)
  2965.             {
  2966.               int dirpos = TellDir(dirptr);
  2967.               while ((dname = ReadDirName(dirptr)))
  2968.                 {
  2969.                   if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2970.                     continue;
  2971.                   if(!IS_VETO_PATH(cnum, dname))
  2972.                     {
  2973.                       all_veto_files = False;
  2974.                       break;
  2975.                     }
  2976.                 }
  2977.               if(all_veto_files)
  2978.                 {
  2979.                   SeekDir(dirptr,dirpos);
  2980.                   while ((dname = ReadDirName(dirptr)))
  2981.                     {
  2982.                       pstring fullname;
  2983.                       struct stat st;
  2984.  
  2985.                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
  2986.                         continue;
  2987.  
  2988.                       /* Construct the full name. */
  2989.                       if(strlen(directory) + strlen(dname) + 1 >= sizeof(fullname))
  2990.                         {
  2991.                           errno = ENOMEM;
  2992.                           break;
  2993.                         }
  2994.                       pstrcpy(fullname, directory);
  2995.                       pstrcat(fullname, "/");
  2996.                       pstrcat(fullname, dname);
  2997.                       
  2998.                       if(sys_lstat(fullname, &st) != 0)
  2999.                         break;
  3000.                       if(st.st_mode & S_IFDIR)
  3001.                       {
  3002.                         if(lp_recursive_veto_delete(SNUM(cnum)))
  3003.                         {
  3004.                           if(recursive_rmdir(fullname) != 0)
  3005.                             break;
  3006.                         }
  3007.                         if(sys_rmdir(fullname) != 0)
  3008.                           break;
  3009.                       }
  3010.                       else if(sys_unlink(fullname) != 0)
  3011.                         break;
  3012.                     }
  3013.                   CloseDir(dirptr);
  3014.                   /* Retry the rmdir */
  3015.                   ok = (sys_rmdir(directory) == 0);
  3016.                 }
  3017.               else
  3018.                 CloseDir(dirptr);
  3019.             }
  3020.           else
  3021.             errno = ENOTEMPTY;
  3022.          }
  3023.           
  3024.       if (!ok)
  3025.         DEBUG(3,("couldn't remove directory %s : %s\n",
  3026.          directory,strerror(errno)));
  3027.     }
  3028.   
  3029.   if (!ok)
  3030.   {
  3031.     if((errno == ENOENT) && bad_path)
  3032.     {
  3033.       unix_ERR_class = ERRDOS;
  3034.       unix_ERR_code = ERRbadpath;
  3035.     }
  3036.     return(UNIXERROR(ERRDOS,ERRbadpath));
  3037.   }
  3038.  
  3039.   outsize = set_message(outbuf,0,0,True);
  3040.   
  3041.   DEBUG(3,("%s rmdir %s\n",timestring(),directory));
  3042.   
  3043.   return(outsize);
  3044. }
  3045.  
  3046.  
  3047. /*******************************************************************
  3048. resolve wildcards in a filename rename
  3049. ********************************************************************/
  3050. static BOOL resolve_wildcards(char *name1,char *name2)
  3051. {
  3052.   fstring root1,root2;
  3053.   fstring ext1,ext2;
  3054.   char *p,*p2;
  3055.  
  3056.   name1 = strrchr(name1,'/');
  3057.   name2 = strrchr(name2,'/');
  3058.  
  3059.   if (!name1 || !name2) return(False);
  3060.   
  3061.   fstrcpy(root1,name1);
  3062.   fstrcpy(root2,name2);
  3063.   p = strrchr(root1,'.');
  3064.   if (p) {
  3065.     *p = 0;
  3066.     fstrcpy(ext1,p+1);
  3067.   } else {
  3068.     fstrcpy(ext1,"");    
  3069.   }
  3070.   p = strrchr(root2,'.');
  3071.   if (p) {
  3072.     *p = 0;
  3073.     fstrcpy(ext2,p+1);
  3074.   } else {
  3075.     fstrcpy(ext2,"");    
  3076.   }
  3077.  
  3078.   p = root1;
  3079.   p2 = root2;
  3080.   while (*p2) {
  3081.     if (*p2 == '?') {
  3082.       *p2 = *p;
  3083.       p2++;
  3084.     } else {
  3085.       p2++;
  3086.     }
  3087.     if (*p) p++;
  3088.   }
  3089.  
  3090.   p = ext1;
  3091.   p2 = ext2;
  3092.   while (*p2) {
  3093.     if (*p2 == '?') {
  3094.       *p2 = *p;
  3095.       p2++;
  3096.     } else {
  3097.       p2++;
  3098.     }
  3099.     if (*p) p++;
  3100.   }
  3101.  
  3102.   fstrcpy(name2,root2);
  3103.   if (ext2[0]) {
  3104.     fstrcat(name2,".");
  3105.     fstrcat(name2,ext2);
  3106.   }
  3107.  
  3108.   return(True);
  3109. }
  3110.  
  3111. /*******************************************************************
  3112. check if a user is allowed to rename a file
  3113. ********************************************************************/
  3114. static BOOL can_rename(char *fname,int cnum)
  3115. {
  3116.   struct stat sbuf;
  3117.  
  3118.   if (!CAN_WRITE(cnum)) return(False);
  3119.  
  3120.   if (sys_lstat(fname,&sbuf) != 0) return(False);
  3121.   if (!check_file_sharing(cnum,fname,True)) return(False);
  3122.  
  3123.   return(True);
  3124. }
  3125.  
  3126. /****************************************************************************
  3127.   reply to a mv
  3128. ****************************************************************************/
  3129. int reply_mv(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3130. {
  3131.   int outsize = 0;
  3132.   pstring name;
  3133.   int cnum;
  3134.   pstring directory;
  3135.   pstring mask,newname;
  3136.   pstring newname_last_component;
  3137.   char *p;
  3138.   int count=0;
  3139.   int error = ERRnoaccess;
  3140.   BOOL has_wild;
  3141.   BOOL exists=False;
  3142.   BOOL bad_path1 = False;
  3143.   BOOL bad_path2 = False;
  3144.  
  3145.   *directory = *mask = 0;
  3146.  
  3147.   cnum = SVAL(inbuf,smb_tid);
  3148.   
  3149.   pstrcpy(name,smb_buf(inbuf) + 1);
  3150.   pstrcpy(newname,smb_buf(inbuf) + 3 + strlen(name));
  3151.    
  3152.   DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
  3153.    
  3154.   unix_convert(name,cnum,0,&bad_path1);
  3155.   unix_convert(newname,cnum,newname_last_component,&bad_path2);
  3156.  
  3157.   /*
  3158.    * Split the old name into directory and last component
  3159.    * strings. Note that unix_convert may have stripped off a 
  3160.    * leading ./ from both name and newname if the rename is 
  3161.    * at the root of the share. We need to make sure either both
  3162.    * name and newname contain a / character or neither of them do
  3163.    * as this is checked in resolve_wildcards().
  3164.    */
  3165.  
  3166.   p = strrchr(name,'/');
  3167.   if (!p) {
  3168.     pstrcpy(directory,".");
  3169.     pstrcpy(mask,name);
  3170.   } else {
  3171.     *p = 0;
  3172.     pstrcpy(directory,name);
  3173.     pstrcpy(mask,p+1);
  3174.     *p = '/'; /* Replace needed for exceptional test below. */
  3175.   }
  3176.  
  3177.   if (is_mangled(mask))
  3178.     check_mangled_stack(mask);
  3179.  
  3180.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  3181.  
  3182.   if (!has_wild) {
  3183.     BOOL is_short_name = is_8_3(name, True);
  3184.  
  3185.     /* Add a terminating '/' to the directory name. */
  3186.     pstrcat(directory,"/");
  3187.     pstrcat(directory,mask);
  3188.  
  3189.     /* Ensure newname contains a '/' also */
  3190.     if(strrchr(newname,'/') == 0) {
  3191.       pstring tmpstr;
  3192.  
  3193.       pstrcpy(tmpstr, "./");
  3194.       pstrcat(tmpstr, newname);
  3195.       pstrcpy(newname, tmpstr);
  3196.     }
  3197.   
  3198.     DEBUG(3,("reply_mv : case_sensitive = %d, case_preserve = %d, short case preserve = %d, directory = %s, newname = %s, newname_last_component = %s, is_8_3 = %d\n", 
  3199.             case_sensitive, case_preserve, short_case_preserve, directory, 
  3200.             newname, newname_last_component, is_short_name));
  3201.  
  3202.     /*
  3203.      * Check for special case with case preserving and not
  3204.      * case sensitive, if directory and newname are identical,
  3205.      * and the old last component differs from the original
  3206.      * last component only by case, then we should allow
  3207.      * the rename (user is trying to change the case of the
  3208.      * filename).
  3209.      */
  3210.     if((case_sensitive == False) && ( ((case_preserve == True) && (is_short_name == False)) || 
  3211.             ((short_case_preserve == True) && (is_short_name == True))) &&
  3212.        strcsequal(directory, newname)) {
  3213.       pstring newname_modified_last_component;
  3214.  
  3215.       /*
  3216.        * Get the last component of the modified name.
  3217.        * Note that we guarantee that newname contains a '/'
  3218.        * character above.
  3219.        */
  3220.       p = strrchr(newname,'/');
  3221.       pstrcpy(newname_modified_last_component,p+1);
  3222.  
  3223.       if(strcsequal(newname_modified_last_component, 
  3224.             newname_last_component) == False) {
  3225.     /*
  3226.      * Replace the modified last component with
  3227.      * the original.
  3228.      */
  3229.         pstrcpy(p+1, newname_last_component);
  3230.       }
  3231.     }
  3232.  
  3233.     if (resolve_wildcards(directory,newname) && 
  3234.     can_rename(directory,cnum) && 
  3235.     !file_exist(newname,NULL) &&
  3236.     !sys_rename(directory,newname)) count++;
  3237.  
  3238.     DEBUG(3,("reply_mv : %s doing rename on %s -> %s\n",(count != 0) ? "succeeded" : "failed",
  3239.                          directory,newname));
  3240.  
  3241.     if (!count) exists = file_exist(directory,NULL);
  3242.     if (!count && exists && file_exist(newname,NULL)) {
  3243.       exists = True;
  3244.       error = 183;
  3245.     }
  3246.   } else {
  3247.     void *dirptr = NULL;
  3248.     char *dname;
  3249.     pstring destname;
  3250.  
  3251.     if (check_name(directory,cnum))
  3252.       dirptr = OpenDir(cnum, directory, True);
  3253.  
  3254.     if (dirptr)
  3255.       {
  3256.     error = ERRbadfile;
  3257.  
  3258.     if (strequal(mask,"????????.???"))
  3259.       pstrcpy(mask,"*");
  3260.  
  3261.     while ((dname = ReadDirName(dirptr)))
  3262.       {
  3263.         pstring fname;
  3264.         pstrcpy(fname,dname);
  3265.         
  3266.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  3267.  
  3268.         error = ERRnoaccess;
  3269.         slprintf(fname,sizeof(fname)-1,"%s/%s",directory,dname);
  3270.         if (!can_rename(fname,cnum)) {
  3271.             DEBUG(6,("rename %s refused\n", fname));
  3272.             continue;
  3273.         }
  3274.         pstrcpy(destname,newname);
  3275.  
  3276.         if (!resolve_wildcards(fname,destname)) {
  3277.             DEBUG(6,("resolve_wildcards %s %s failed\n", 
  3278.                  fname, destname));
  3279.             continue;
  3280.         }
  3281.  
  3282.         if (file_exist(destname,NULL)) {
  3283.             DEBUG(6,("file_exist %s\n", 
  3284.                  destname));
  3285.             error = 183;
  3286.             continue;
  3287.         }
  3288.         if (!sys_rename(fname,destname)) count++;
  3289.         DEBUG(3,("reply_mv : doing rename on %s -> %s\n",fname,destname));
  3290.       }
  3291.     CloseDir(dirptr);
  3292.       }
  3293.   }
  3294.   
  3295.   if (count == 0) {
  3296.     if (exists)
  3297.       return(ERROR(ERRDOS,error));
  3298.     else
  3299.     {
  3300.       if((errno == ENOENT) && (bad_path1 || bad_path2))
  3301.       {
  3302.         unix_ERR_class = ERRDOS;
  3303.         unix_ERR_code = ERRbadpath;
  3304.       }
  3305.       return(UNIXERROR(ERRDOS,error));
  3306.     }
  3307.   }
  3308.   
  3309.   outsize = set_message(outbuf,0,0,True);
  3310.   
  3311.   return(outsize);
  3312. }
  3313.  
  3314. /*******************************************************************
  3315.   copy a file as part of a reply_copy
  3316.   ******************************************************************/
  3317. static BOOL copy_file(char *src,char *dest1,int cnum,int ofun,
  3318.               int count,BOOL target_is_directory)
  3319. {
  3320.   int Access,action;
  3321.   struct stat st;
  3322.   int ret=0;
  3323.   int fnum1,fnum2;
  3324.   pstring dest;
  3325.   
  3326.   pstrcpy(dest,dest1);
  3327.   if (target_is_directory) {
  3328.     char *p = strrchr(src,'/');
  3329.     if (p) 
  3330.       p++;
  3331.     else
  3332.       p = src;
  3333.     pstrcat(dest,"/");
  3334.     pstrcat(dest,p);
  3335.   }
  3336.  
  3337.   if (!file_exist(src,&st)) return(False);
  3338.  
  3339.   fnum1 = find_free_file();
  3340.   if (fnum1<0) return(False);
  3341.   open_file_shared(fnum1,cnum,src,(DENY_NONE<<4),
  3342.            1,0,0,&Access,&action);
  3343.  
  3344.   if (!Files[fnum1].open) {
  3345.       Files[fnum1].reserved = False;
  3346.       return(False);
  3347.   }
  3348.  
  3349.   if (!target_is_directory && count)
  3350.     ofun = 1;
  3351.  
  3352.   fnum2 = find_free_file();
  3353.   if (fnum2<0) {
  3354.     close_file(fnum1,False);
  3355.     return(False);
  3356.   }
  3357.   open_file_shared(fnum2,cnum,dest,(DENY_NONE<<4)|1,
  3358.            ofun,st.st_mode,0,&Access,&action);
  3359.  
  3360.   if (!Files[fnum2].open) {
  3361.     close_file(fnum1,False);
  3362.     Files[fnum2].reserved = False;
  3363.     return(False);
  3364.   }
  3365.  
  3366.   if ((ofun&3) == 1) {
  3367.     lseek(Files[fnum2].fd_ptr->fd,0,SEEK_END);
  3368.   }
  3369.   
  3370.   if (st.st_size)
  3371.     ret = transfer_file(Files[fnum1].fd_ptr->fd,Files[fnum2].fd_ptr->fd,st.st_size,NULL,0,0);
  3372.  
  3373.   close_file(fnum1,False);
  3374.   close_file(fnum2,False);
  3375.  
  3376.   return(ret == st.st_size);
  3377. }
  3378.  
  3379.  
  3380.  
  3381. /****************************************************************************
  3382.   reply to a file copy.
  3383.   ****************************************************************************/
  3384. int reply_copy(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3385. {
  3386.   int outsize = 0;
  3387.   pstring name;
  3388.   int cnum;
  3389.   pstring directory;
  3390.   pstring mask,newname;
  3391.   char *p;
  3392.   int count=0;
  3393.   int error = ERRnoaccess;
  3394.   BOOL has_wild;
  3395.   BOOL exists=False;
  3396.   int tid2 = SVAL(inbuf,smb_vwv0);
  3397.   int ofun = SVAL(inbuf,smb_vwv1);
  3398.   int flags = SVAL(inbuf,smb_vwv2);
  3399.   BOOL target_is_directory=False;
  3400.   BOOL bad_path1 = False;
  3401.   BOOL bad_path2 = False;
  3402.  
  3403.   *directory = *mask = 0;
  3404.  
  3405.   cnum = SVAL(inbuf,smb_tid);
  3406.   
  3407.   pstrcpy(name,smb_buf(inbuf));
  3408.   pstrcpy(newname,smb_buf(inbuf) + 1 + strlen(name));
  3409.    
  3410.   DEBUG(3,("reply_copy : %s -> %s\n",name,newname));
  3411.    
  3412.   if (tid2 != cnum) {
  3413.     /* can't currently handle inter share copies XXXX */
  3414.     DEBUG(3,("Rejecting inter-share copy\n"));
  3415.     return(ERROR(ERRSRV,ERRinvdevice));
  3416.   }
  3417.  
  3418.   unix_convert(name,cnum,0,&bad_path1);
  3419.   unix_convert(newname,cnum,0,&bad_path2);
  3420.  
  3421.   target_is_directory = directory_exist(newname,NULL);
  3422.  
  3423.   if ((flags&1) && target_is_directory) {
  3424.     return(ERROR(ERRDOS,ERRbadfile));
  3425.   }
  3426.  
  3427.   if ((flags&2) && !target_is_directory) {
  3428.     return(ERROR(ERRDOS,ERRbadpath));
  3429.   }
  3430.  
  3431.   if ((flags&(1<<5)) && directory_exist(name,NULL)) {
  3432.     /* wants a tree copy! XXXX */
  3433.     DEBUG(3,("Rejecting tree copy\n"));
  3434.     return(ERROR(ERRSRV,ERRerror));    
  3435.   }
  3436.  
  3437.   p = strrchr(name,'/');
  3438.   if (!p) {
  3439.     pstrcpy(directory,"./");
  3440.     pstrcpy(mask,name);
  3441.   } else {
  3442.     *p = 0;
  3443.     pstrcpy(directory,name);
  3444.     pstrcpy(mask,p+1);
  3445.   }
  3446.  
  3447.   if (is_mangled(mask))
  3448.     check_mangled_stack(mask);
  3449.  
  3450.   has_wild = strchr(mask,'*') || strchr(mask,'?');
  3451.  
  3452.   if (!has_wild) {
  3453.     pstrcat(directory,"/");
  3454.     pstrcat(directory,mask);
  3455.     if (resolve_wildcards(directory,newname) && 
  3456.     copy_file(directory,newname,cnum,ofun,
  3457.           count,target_is_directory)) count++;
  3458.     if (!count) exists = file_exist(directory,NULL);
  3459.   } else {
  3460.     void *dirptr = NULL;
  3461.     char *dname;
  3462.     pstring destname;
  3463.  
  3464.     if (check_name(directory,cnum))
  3465.       dirptr = OpenDir(cnum, directory, True);
  3466.  
  3467.     if (dirptr)
  3468.       {
  3469.     error = ERRbadfile;
  3470.  
  3471.     if (strequal(mask,"????????.???"))
  3472.       pstrcpy(mask,"*");
  3473.  
  3474.     while ((dname = ReadDirName(dirptr)))
  3475.       {
  3476.         pstring fname;
  3477.         pstrcpy(fname,dname);
  3478.         
  3479.         if(!mask_match(fname, mask, case_sensitive, False)) continue;
  3480.  
  3481.         error = ERRnoaccess;
  3482.         slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
  3483.         pstrcpy(destname,newname);
  3484.         if (resolve_wildcards(fname,destname) && 
  3485.         copy_file(directory,newname,cnum,ofun,
  3486.               count,target_is_directory)) count++;
  3487.         DEBUG(3,("reply_copy : doing copy on %s -> %s\n",fname,destname));
  3488.       }
  3489.     CloseDir(dirptr);
  3490.       }
  3491.   }
  3492.   
  3493.   if (count == 0) {
  3494.     if (exists)
  3495.       return(ERROR(ERRDOS,error));
  3496.     else
  3497.     {
  3498.       if((errno == ENOENT) && (bad_path1 || bad_path2))
  3499.       {
  3500.         unix_ERR_class = ERRDOS;
  3501.         unix_ERR_code = ERRbadpath;
  3502.       }
  3503.       return(UNIXERROR(ERRDOS,error));
  3504.     }
  3505.   }
  3506.   
  3507.   outsize = set_message(outbuf,1,0,True);
  3508.   SSVAL(outbuf,smb_vwv0,count);
  3509.  
  3510.   return(outsize);
  3511. }
  3512.  
  3513.  
  3514.  
  3515. /****************************************************************************
  3516.   reply to a setdir
  3517. ****************************************************************************/
  3518. int reply_setdir(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3519. {
  3520.   int cnum,snum;
  3521.   int outsize = 0;
  3522.   BOOL ok = False;
  3523.   pstring newdir;
  3524.   
  3525.   cnum = SVAL(inbuf,smb_tid);
  3526.   
  3527.   snum = Connections[cnum].service;
  3528.   if (!CAN_SETDIR(snum))
  3529.     return(ERROR(ERRDOS,ERRnoaccess));
  3530.   
  3531.   pstrcpy(newdir,smb_buf(inbuf) + 1);
  3532.   strlower(newdir);
  3533.   
  3534.   if (strlen(newdir) == 0)
  3535.     ok = True;
  3536.   else
  3537.     {
  3538.       ok = directory_exist(newdir,NULL);
  3539.       if (ok)
  3540.     string_set(&Connections[cnum].connectpath,newdir);
  3541.     }
  3542.   
  3543.   if (!ok)
  3544.     return(ERROR(ERRDOS,ERRbadpath));
  3545.   
  3546.   outsize = set_message(outbuf,0,0,True);
  3547.   CVAL(outbuf,smb_reh) = CVAL(inbuf,smb_reh);
  3548.   
  3549.   DEBUG(3,("%s setdir %s cnum=%d\n",timestring(),newdir,cnum));
  3550.   
  3551.   return(outsize);
  3552. }
  3553.  
  3554.  
  3555. /****************************************************************************
  3556.   reply to a lockingX request
  3557. ****************************************************************************/
  3558. int reply_lockingX(char *inbuf,char *outbuf,int length,int bufsize)
  3559. {
  3560.   int fnum = GETFNUM(inbuf,smb_vwv2);
  3561.   unsigned char locktype = CVAL(inbuf,smb_vwv3);
  3562. #if 0
  3563.   unsigned char oplocklevel = CVAL(inbuf,smb_vwv3+1);
  3564. #endif
  3565.   uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
  3566.   uint16 num_locks = SVAL(inbuf,smb_vwv7);
  3567.   uint32 count, offset;
  3568.  
  3569.   int cnum;
  3570.   int i;
  3571.   char *data;
  3572.   uint32 ecode=0, dummy2;
  3573.   int eclass=0, dummy1;
  3574.  
  3575.   cnum = SVAL(inbuf,smb_tid);
  3576.  
  3577.   CHECK_FNUM(fnum,cnum);
  3578.   CHECK_ERROR(fnum);
  3579.  
  3580.   data = smb_buf(inbuf);
  3581.  
  3582.   /* Check if this is an oplock break on a file
  3583.      we have granted an oplock on.
  3584.    */
  3585.   if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE))
  3586.   {
  3587.     int token;
  3588.     files_struct *fsp = &Files[fnum];
  3589.     uint32 dev = fsp->fd_ptr->dev;
  3590.     uint32 inode = fsp->fd_ptr->inode;
  3591.  
  3592.     DEBUG(5,("reply_lockingX: oplock break reply from client for fnum = %d\n",
  3593.               fnum));
  3594.     /*
  3595.      * Make sure we have granted an oplock on this file.
  3596.      */
  3597.     if(!fsp->granted_oplock)
  3598.     {
  3599.       DEBUG(0,("reply_lockingX: Error : oplock break from client for fnum = %d and \
  3600. no oplock granted on this file.\n", fnum));
  3601.       return ERROR(ERRDOS,ERRlock);
  3602.     }
  3603.  
  3604.     /* Remove the oplock flag from the sharemode. */
  3605.     lock_share_entry(fsp->cnum, dev, inode, &token);
  3606.     if(remove_share_oplock( fnum, token)==False) {
  3607.         DEBUG(0,("reply_lockingX: failed to remove share oplock for fnum %d, \
  3608. dev = %x, inode = %x\n", 
  3609.              fnum, dev, inode));
  3610.         unlock_share_entry(fsp->cnum, dev, inode, token);
  3611.     } else {
  3612.         unlock_share_entry(fsp->cnum, dev, inode, token);
  3613.  
  3614.         /* Clear the granted flag and return. */
  3615.         fsp->granted_oplock = False;
  3616.     }
  3617.  
  3618.     /* if this is a pure oplock break request then don't send a reply */
  3619.     if (num_locks == 0 && num_ulocks == 0)
  3620.     {
  3621.       /* Sanity check - ensure a pure oplock break is not a
  3622.          chained request. */
  3623.       if(CVAL(inbuf,smb_vwv0) != 0xff)
  3624.         DEBUG(0,("reply_lockingX: Error : pure oplock break is a chained %d request !\n",
  3625.                  (unsigned int)CVAL(inbuf,smb_vwv0) ));
  3626.       return -1;
  3627.     }
  3628.   }
  3629.  
  3630.   /* Data now points at the beginning of the list
  3631.      of smb_unlkrng structs */
  3632.   for(i = 0; i < (int)num_ulocks; i++) {
  3633.     count = IVAL(data,SMB_LKLEN_OFFSET(i));
  3634.     offset = IVAL(data,SMB_LKOFF_OFFSET(i));
  3635.     if(!do_unlock(fnum,cnum,count,offset,&eclass, &ecode))
  3636.       return ERROR(eclass,ecode);
  3637.   }
  3638.  
  3639.   /* Now do any requested locks */
  3640.   data += 10*num_ulocks;
  3641.   /* Data now points at the beginning of the list
  3642.      of smb_lkrng structs */
  3643.   for(i = 0; i < (int)num_locks; i++) {
  3644.     count = IVAL(data,SMB_LKLEN_OFFSET(i)); 
  3645.     offset = IVAL(data,SMB_LKOFF_OFFSET(i)); 
  3646.     if(!do_lock(fnum,cnum,count,offset, ((locktype & 1) ? F_RDLCK : F_WRLCK),
  3647.                 &eclass, &ecode))
  3648.       break;
  3649.   }
  3650.  
  3651.   /* If any of the above locks failed, then we must unlock
  3652.      all of the previous locks (X/Open spec). */
  3653.   if(i != num_locks && num_locks != 0) {
  3654.     for(; i >= 0; i--) {
  3655.       count = IVAL(data,SMB_LKLEN_OFFSET(i));  
  3656.       offset = IVAL(data,SMB_LKOFF_OFFSET(i)); 
  3657.       do_unlock(fnum,cnum,count,offset,&dummy1,&dummy2);
  3658.     }
  3659.     return ERROR(eclass,ecode);
  3660.   }
  3661.  
  3662.   set_message(outbuf,2,0,True);
  3663.   
  3664.   DEBUG(3,("%s lockingX fnum=%d cnum=%d type=%d num_locks=%d num_ulocks=%d\n",
  3665.     timestring(),fnum,cnum,(unsigned int)locktype,num_locks,num_ulocks));
  3666.  
  3667.   chain_fnum = fnum;
  3668.  
  3669.   return chain_reply(inbuf,outbuf,length,bufsize);
  3670. }
  3671.  
  3672.  
  3673. /****************************************************************************
  3674.   reply to a SMBreadbmpx (read block multiplex) request
  3675. ****************************************************************************/
  3676. int reply_readbmpx(char *inbuf,char *outbuf,int length,int bufsize)
  3677. {
  3678.   int cnum,fnum;
  3679.   int nread = -1;
  3680.   int total_read;
  3681.   char *data;
  3682.   uint32 startpos;
  3683.   int outsize, mincount, maxcount;
  3684.   int max_per_packet;
  3685.   int tcount;
  3686.   int pad;
  3687.  
  3688.   /* this function doesn't seem to work - disable by default */
  3689.   if (!lp_readbmpx())
  3690.     return(ERROR(ERRSRV,ERRuseSTD));
  3691.  
  3692.   outsize = set_message(outbuf,8,0,True);
  3693.  
  3694.   cnum = SVAL(inbuf,smb_tid);
  3695.   fnum = GETFNUM(inbuf,smb_vwv0);
  3696.  
  3697.   CHECK_FNUM(fnum,cnum);
  3698.   CHECK_READ(fnum);
  3699.   CHECK_ERROR(fnum);
  3700.  
  3701.   startpos = IVAL(inbuf,smb_vwv1);
  3702.   maxcount = SVAL(inbuf,smb_vwv3);
  3703.   mincount = SVAL(inbuf,smb_vwv4);
  3704.  
  3705.   data = smb_buf(outbuf);
  3706.   pad = ((long)data)%4;
  3707.   if (pad) pad = 4 - pad;
  3708.   data += pad;
  3709.  
  3710.   max_per_packet = bufsize-(outsize+pad);
  3711.   tcount = maxcount;
  3712.   total_read = 0;
  3713.  
  3714.   if (is_locked(fnum,cnum,maxcount,startpos,F_RDLCK))
  3715.     return(ERROR(ERRDOS,ERRlock));
  3716.     
  3717.   do
  3718.     {
  3719.       int N = MIN(max_per_packet,tcount-total_read);
  3720.   
  3721.       nread = read_file(fnum,data,startpos,N);
  3722.  
  3723.       if (nread <= 0) nread = 0;
  3724.  
  3725.       if (nread < N)
  3726.     tcount = total_read + nread;
  3727.  
  3728.       set_message(outbuf,8,nread,False);
  3729.       SIVAL(outbuf,smb_vwv0,startpos);
  3730.       SSVAL(outbuf,smb_vwv2,tcount);
  3731.       SSVAL(outbuf,smb_vwv6,nread);
  3732.       SSVAL(outbuf,smb_vwv7,smb_offset(data,outbuf));
  3733.  
  3734.       send_smb(Client,outbuf);
  3735.  
  3736.       total_read += nread;
  3737.       startpos += nread;
  3738.     }
  3739.   while (total_read < tcount);
  3740.  
  3741.   return(-1);
  3742. }
  3743.  
  3744.  
  3745. /****************************************************************************
  3746.   reply to a SMBwritebmpx (write block multiplex primary) request
  3747. ****************************************************************************/
  3748. int reply_writebmpx(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3749. {
  3750.   int cnum,numtowrite,fnum;
  3751.   int nwritten = -1;
  3752.   int outsize = 0;
  3753.   uint32 startpos;
  3754.   int tcount, write_through, smb_doff;
  3755.   char *data;
  3756.   
  3757.   cnum = SVAL(inbuf,smb_tid);
  3758.   fnum = GETFNUM(inbuf,smb_vwv0);
  3759.  
  3760.   CHECK_FNUM(fnum,cnum);
  3761.   CHECK_WRITE(fnum);
  3762.   CHECK_ERROR(fnum);
  3763.  
  3764.   tcount = SVAL(inbuf,smb_vwv1);
  3765.   startpos = IVAL(inbuf,smb_vwv3);
  3766.   write_through = BITSETW(inbuf+smb_vwv7,0);
  3767.   numtowrite = SVAL(inbuf,smb_vwv10);
  3768.   smb_doff = SVAL(inbuf,smb_vwv11);
  3769.  
  3770.   data = smb_base(inbuf) + smb_doff;
  3771.  
  3772.   /* If this fails we need to send an SMBwriteC response,
  3773.      not an SMBwritebmpx - set this up now so we don't forget */
  3774.   CVAL(outbuf,smb_com) = SMBwritec;
  3775.  
  3776.   if (is_locked(fnum,cnum,tcount,startpos,F_WRLCK))
  3777.     return(ERROR(ERRDOS,ERRlock));
  3778.  
  3779.   seek_file(fnum,startpos);
  3780.   nwritten = write_file(fnum,data,numtowrite);
  3781.  
  3782.   if(lp_syncalways(SNUM(cnum)) || write_through)
  3783.     sync_file(cnum,fnum);
  3784.   
  3785.   if(nwritten < numtowrite)
  3786.     return(UNIXERROR(ERRHRD,ERRdiskfull));
  3787.  
  3788.   /* If the maximum to be written to this file
  3789.      is greater than what we just wrote then set
  3790.      up a secondary struct to be attached to this
  3791.      fd, we will use this to cache error messages etc. */
  3792.   if(tcount > nwritten) 
  3793.     {
  3794.       write_bmpx_struct *wbms;
  3795.       if(Files[fnum].wbmpx_ptr != NULL)
  3796.     wbms = Files[fnum].wbmpx_ptr; /* Use an existing struct */
  3797.       else
  3798.     wbms = (write_bmpx_struct *)malloc(sizeof(write_bmpx_struct));
  3799.       if(!wbms)
  3800.     {
  3801.       DEBUG(0,("Out of memory in reply_readmpx\n"));
  3802.       return(ERROR(ERRSRV,ERRnoresource));
  3803.     }
  3804.       wbms->wr_mode = write_through;
  3805.       wbms->wr_discard = False; /* No errors yet */
  3806.       wbms->wr_total_written = nwritten;
  3807.       wbms->wr_errclass = 0;
  3808.       wbms->wr_error = 0;
  3809.       Files[fnum].wbmpx_ptr = wbms;
  3810.     }
  3811.  
  3812.   /* We are returning successfully, set the message type back to
  3813.      SMBwritebmpx */
  3814.   CVAL(outbuf,smb_com) = SMBwriteBmpx;
  3815.   
  3816.   outsize = set_message(outbuf,1,0,True);
  3817.   
  3818.   SSVALS(outbuf,smb_vwv0,-1); /* We don't support smb_remaining */
  3819.   
  3820.   DEBUG(3,("%s writebmpx fnum=%d cnum=%d num=%d wrote=%d\n",
  3821.     timestring(),fnum,cnum,numtowrite,nwritten));
  3822.   
  3823.   if (write_through && tcount==nwritten) {
  3824.     /* we need to send both a primary and a secondary response */
  3825.     smb_setlen(outbuf,outsize - 4);
  3826.     send_smb(Client,outbuf);
  3827.  
  3828.     /* now the secondary */
  3829.     outsize = set_message(outbuf,1,0,True);
  3830.     CVAL(outbuf,smb_com) = SMBwritec;
  3831.     SSVAL(outbuf,smb_vwv0,nwritten);
  3832.   }
  3833.  
  3834.   return(outsize);
  3835. }
  3836.  
  3837.  
  3838. /****************************************************************************
  3839.   reply to a SMBwritebs (write block multiplex secondary) request
  3840. ****************************************************************************/
  3841. int reply_writebs(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3842. {
  3843.   int cnum,numtowrite,fnum;
  3844.   int nwritten = -1;
  3845.   int outsize = 0;
  3846.   int32 startpos;
  3847.   int tcount, write_through, smb_doff;
  3848.   char *data;
  3849.   write_bmpx_struct *wbms;
  3850.   BOOL send_response = False;
  3851.   
  3852.   cnum = SVAL(inbuf,smb_tid);
  3853.   fnum = GETFNUM(inbuf,smb_vwv0);
  3854.   CHECK_FNUM(fnum,cnum);
  3855.   CHECK_WRITE(fnum);
  3856.  
  3857.   tcount = SVAL(inbuf,smb_vwv1);
  3858.   startpos = IVAL(inbuf,smb_vwv2);
  3859.   numtowrite = SVAL(inbuf,smb_vwv6);
  3860.   smb_doff = SVAL(inbuf,smb_vwv7);
  3861.  
  3862.   data = smb_base(inbuf) + smb_doff;
  3863.  
  3864.   /* We need to send an SMBwriteC response, not an SMBwritebs */
  3865.   CVAL(outbuf,smb_com) = SMBwritec;
  3866.  
  3867.   /* This fd should have an auxiliary struct attached,
  3868.      check that it does */
  3869.   wbms = Files[fnum].wbmpx_ptr;
  3870.   if(!wbms) return(-1);
  3871.  
  3872.   /* If write through is set we can return errors, else we must
  3873.      cache them */
  3874.   write_through = wbms->wr_mode;
  3875.  
  3876.   /* Check for an earlier error */
  3877.   if(wbms->wr_discard)
  3878.     return -1; /* Just discard the packet */
  3879.  
  3880.   seek_file(fnum,startpos);
  3881.   nwritten = write_file(fnum,data,numtowrite);
  3882.  
  3883.   if(lp_syncalways(SNUM(cnum)) || write_through)
  3884.     sync_file(cnum,fnum);
  3885.   
  3886.   if (nwritten < numtowrite)
  3887.     {
  3888.       if(write_through)    {
  3889.     /* We are returning an error - we can delete the aux struct */
  3890.     if (wbms) free((char *)wbms);
  3891.     Files[fnum].wbmpx_ptr = NULL;
  3892.     return(ERROR(ERRHRD,ERRdiskfull));
  3893.       }
  3894.       return(CACHE_ERROR(wbms,ERRHRD,ERRdiskfull));
  3895.     }
  3896.  
  3897.   /* Increment the total written, if this matches tcount
  3898.      we can discard the auxiliary struct (hurrah !) and return a writeC */
  3899.   wbms->wr_total_written += nwritten;
  3900.   if(wbms->wr_total_written >= tcount)
  3901.     {
  3902.       if (write_through) {
  3903.     outsize = set_message(outbuf,1,0,True);
  3904.     SSVAL(outbuf,smb_vwv0,wbms->wr_total_written);    
  3905.     send_response = True;
  3906.       }
  3907.  
  3908.       free((char *)wbms);
  3909.       Files[fnum].wbmpx_ptr = NULL;
  3910.     }
  3911.  
  3912.   if(send_response)
  3913.     return(outsize);
  3914.  
  3915.   return(-1);
  3916. }
  3917.  
  3918.  
  3919. /****************************************************************************
  3920.   reply to a SMBsetattrE
  3921. ****************************************************************************/
  3922. int reply_setattrE(char *inbuf,char *outbuf,int dum_size, int dum_buffsize)
  3923. {
  3924.   int cnum,fnum;
  3925.   struct utimbuf unix_times;
  3926.   int outsize = 0;
  3927.  
  3928.   outsize = set_message(outbuf,0,0,True);
  3929.  
  3930.   cnum = SVAL(inbuf,smb_tid);
  3931.   fnum = GETFNUM(inbuf,smb_vwv0);
  3932.  
  3933.   CHECK_FNUM(fnum,cnum);
  3934.   CHECK_ERROR(fnum);
  3935.  
  3936.   /* Convert the DOS times into unix times. Ignore create
  3937.      time as UNIX can't set this.
  3938.      */
  3939.   unix_times.actime = make_unix_date2(inbuf+smb_vwv3);
  3940.   unix_times.modtime = make_unix_date2(inbuf+smb_vwv5);
  3941.   
  3942.   /* 
  3943.    * Patch from Ray Frush <frush@engr.colostate.edu>
  3944.    * Sometimes times are sent as zero - ignore them.
  3945.    */
  3946.  
  3947.   if ((unix_times.actime == 0) && (unix_times.modtime == 0)) 
  3948.   {
  3949.     /* Ignore request */
  3950.     DEBUG(3,("%s reply_setattrE fnum=%d cnum=%d ignoring zero request - \
  3951. not setting timestamps of 0\n",
  3952.           timestring(), fnum,cnum,unix_times.actime,unix_times.modtime));
  3953.     return(outsize);
  3954.   }
  3955.   else if ((unix_times.actime != 0) && (unix_times.modtime == 0)) 
  3956.   {
  3957.     /* set modify time = to access time if modify time was 0 */
  3958.     unix_times.modtime = unix_times.actime;
  3959.   }
  3960.  
  3961.   /* Set the date on this file */
  3962.   if(file_utime(cnum, Files[fnum].name, &unix_times))
  3963.     return(ERROR(ERRDOS,ERRnoaccess));
  3964.   
  3965.   DEBUG(3,("%s reply_setattrE fnum=%d cnum=%d actime=%d modtime=%d\n",
  3966.     timestring(), fnum,cnum,unix_times.actime,unix_times.modtime));
  3967.  
  3968.   return(outsize);
  3969. }
  3970.  
  3971.  
  3972. /****************************************************************************
  3973.   reply to a SMBgetattrE
  3974. ****************************************************************************/
  3975. int reply_getattrE(char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
  3976. {
  3977.   int cnum,fnum;
  3978.   struct stat sbuf;
  3979.   int outsize = 0;
  3980.   int mode;
  3981.  
  3982.   outsize = set_message(outbuf,11,0,True);
  3983.  
  3984.   cnum = SVAL(inbuf,smb_tid);
  3985.   fnum = GETFNUM(inbuf,smb_vwv0);
  3986.  
  3987.   CHECK_FNUM(fnum,cnum);
  3988.   CHECK_ERROR(fnum);
  3989.  
  3990.   /* Do an fstat on this file */
  3991.   if(fstat(Files[fnum].fd_ptr->fd, &sbuf))
  3992.     return(UNIXERROR(ERRDOS,ERRnoaccess));
  3993.   
  3994.   mode = dos_mode(cnum,Files[fnum].name,&sbuf);
  3995.   
  3996.   /* Convert the times into dos times. Set create
  3997.      date to be last modify date as UNIX doesn't save
  3998.      this */
  3999.   put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(cnum))));
  4000.   put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
  4001.   put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
  4002.   if (mode & aDIR)
  4003.     {
  4004.       SIVAL(outbuf,smb_vwv6,0);
  4005.       SIVAL(outbuf,smb_vwv8,0);
  4006.     }
  4007.   else
  4008.     {
  4009.       SIVAL(outbuf,smb_vwv6,sbuf.st_size);
  4010.       SIVAL(outbuf,smb_vwv8,ROUNDUP(sbuf.st_size,1024));
  4011.     }
  4012.   SSVAL(outbuf,smb_vwv10, mode);
  4013.   
  4014.   DEBUG(3,("%s reply_getattrE fnum=%d cnum=%d\n",timestring(),fnum,cnum));
  4015.   
  4016.   return(outsize);
  4017. }
  4018.